1.1.5 • Published 1 year ago

typesafe-property-paths v1.1.5

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

typesafe-property-paths

Helpful for creating typesafe paths to properties in an object with semi-support for arrays.

interface Object {
  firstName: 'jakob';
  lastName: string;
  children?: {
    firstName: string;
  }[];
}

type Paths = PropertyStringPath<Object>;
// type is "firstName" | "lastName" | "children" | "children[${number}]" | "children[${number}].firstName"

Helper functions

ValueAtPath Can extract the type at a given path

interface Object {
  name: {
    firstName: 'jakob';
    lastName: string | null;
  };
  favoriteColors: string[];
  children?: {
    name: {
      firstName: string;
      lastName?: string;
    };
  }[];
}

type LastName = ValueAtPath<Object, 'name.lastName'>;
// type is string | null

type ChildFirstName = ValueAtPath<Object, 'children[0].name.firstName'>;
// type is string | undefined
import { concat } from 'typesafe-property-paths';

const first = 'hello'; // type is 'hello'
const second = 'world'; //type is 'world'

const example = first + second; // type is string

const example2 = concat(first, second); // type is 'helloworld'
1.1.1

2 years ago

1.1.0

2 years ago

1.1.5

1 year ago

1.1.4

1 year ago

1.1.3

2 years ago

1.1.2

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago