Types I'm tired of rewriting.
- Utility Types
- Syntactic Sugar Types
Get the path keys of an object as string literals.
const myObj = {
foo: {
bar: "hi",
baz: "hello",
},
};
type MyObjPath = ObjectPath<typeof myObj>; // "foo" | "foo.bar" | "foo.baz"
Get the path keys of an object that contains a certain key.
const myObj = {
foo: {
bar: {
baz: "hi",
},
},
};
type findBazPath = ObjectPathContainsKey<typeof myObj, "baz">; // "foo.bar"
Get the value of an object at a certain path (using dot notation)
const myObj = {
foo: {
bar: {
baz: "hi",
},
},
} as const;
type baz = ValueAtPath<typeof myObj, "foo.bar.baz">; // "hi"
Make all properties and nested properties of an object optional.
Get the type of an array item.
Define a tuple with exactly 2 elements of types T
and N
.
const tuple: Tuple<string, number> = ["foo", 1];
Define a tuple with a variable number of elements.
const vTuple: VariadicTuple<string, number, boolean> = ["foo", 1, true];
Define a type that can be null
or of type T
.
Make all properties of an object required, removing optional modifiers.
Omit keys of an object that have function values.
Pick keys of an object that have Type
values
const obj = { foo: "bar", baz: 42 };
type MyKeys = PickKeysByValue<typeof obj, string>; // "foo"
Pick keys of an object that have function values.
Pick keys of an object that have object values.
Pick keys of an object that have array values.
Pick keys of an object that have string values.
Pick keys of an object that have number values.
Remove readonly modifier from all properties of an object
Make all properties of an object readonly
Primitive types. Builds on top of Comparables
.
Json types.
A Json object consists of a JsonObject
or JsonArray
, which in turn consist of JsonValue
s.
A Json array consists of JsonValue
s.
A Json value can be a string
, number
, boolean
, null
, JsonObject
, or JsonArray
.
Primitive types that can be compared, including number
, string
, and BigInteger
.
A function that returns type T
or void
.
const myCallback: Callback<string> = () => "foo";
A function that returns type T
or void
and takes arguments of type Args
.
const myCallback: CallbackWithArgs<string, [number, string]> = (num, str) =>
"foo";
A result type that can be either a success or an error.
const myResult: Result<string, number> = { success: true, value: "foo" };
const myResult: Result<string, number> = { success: false, error: 101 };
const myDict: Dictionary<string> = { foo: "bar" };
For state management systems like Redux, representing an action with a type and payload.
type Actions = Action<"INCREMENT", number> | Action<"DECREMENT", number>;