Here is a library with some useful TypeScript types.
Converts URL parameters to an object. It can also escape character sequences.
type Test1 = URLParamsToObject<"a=2">
// { a: "2" }
type Test2 = URLParamsToObject<"a=2&b=hi">
// { a: "2", b: "hi" }
type Test3 = URLParamsToObject<"a=1%20foo&b=2">
// { a: "1%20foo", b: "2" }
type Test4 = URLParamsToObject<"a=1%20foo&b=2", { decodeEncodings: true }>
// { a: "1 foo", b: "2" }
type Test5 = URLParamsToObject<"a=1&a=2", { duplicateKeys: "array" }>
// { a: ["1", "2"] }You can pass in another type parameter for options on how would you like to parse the parameters. The following options are available:
Whether to decode the encodings (i.e. %20 to " ", %21 to "!", etc.). The default value is false.
type NoDecoding = URLParamsToObject<"a=hello%20there"> // { a: "hello%20there" }
type Decoding = URLParamsToObject<"a=hello%20there", { decodeEncodings: true }> // { a: "hello there" }What to do with with duplicate keys?
"first": This will get the value of first duplicate key"last": This will get the value of last duplicate key"array": This will put all duplicate keys' values in an array
The default value is "first".
type first = URLParamsToObject<"a=2&a=5">
// { a: "2" }
type last = URLParamsToObject<"a=2&a=5", { duplicateKeys: "last" }>
// { a: "5" }
type array = URLParamsToObject<"a=2&a=5", { duplicateKeys: "array" }>
// { a: ["2", "5"] }Extract URL parameters as an object from a full or partial URL.
type Test1 = ExtractURLParams<"https://example.com?a=1">
// { a: "1" }
type Test2 = ExtractURLParams<"https://example.com?a=1&b=hi">
// { a: "1", b: "hi" }It can also take the same options as URLParamsToObject.
type Test3 = ExtractURLParams<"https://example.com?a=1%20foo&b=2">
// { a: "1%20foo", b: "2" }
type Test4 = ExtractURLParams<"https://example.com?a=1%20foo&b=2", { decodeEncodings: true }>
// { a: "1 foo", b: "2" }
type Test5 = ExtractURLParams<"https://example.com?a=2&a=5", { duplicateKeys: "last" }>
// { a: "5" }Concatenates all the elements in an array. It can only concatenate an array of stringable elements. The stringable elements are string, number, bigint, boolean, null, and undefined.
type concatArray = ConcatArray<["123", "456", "789"]>
// "123456789"Makes all properties in an object optional and never, which means that the properties cannot be used at all.
type never = Never<{ a: string, b?: number }>
// { a?: never, b?: never }Removes the properties of U from T.
type aAndb = {
a: string
b: number
}
type b = {
b: number
}
type a = WithoutProps<aAndb, b>
// { a: string }Uses one of a set amount of objects, but not multiple ones.
type a = {
a: string
}
type b = {
b: number
}
type OneOfAOrB = OneOf2<a, b>
// ({ a: string } & { b?: never }) | ({ b: number } & { a?: never })
const test1: OneOfAOrB = { a: "hi" } // Valid ✅
const test2: OneOfAOrB = { b: 123 } // Valid ✅
const test3: OneOfAOrB = { a: "hi", b: 123 } // Invalid ❌
const test4: OneOfAOrB = {} // Invalid ❌There is also OneOf3 for 3 objects.
Whether a string literal is a complete string literal. Meaning that all of it characters are a known character and neither string, number, nor bigint.
type TestNothing = IsCompleteStringLiteral<`Hello`>
// true
type TestString = IsCompleteStringLiteral<`Hello, ${string}`>
// false
type TestNumber = IsCompleteStringLiteral<`He has ${number}$`>
// false
type TestBigInt = IsCompleteStringLiteral<`The size of the file is ${bigint} Kilobytes`>
// false
type TestBoolean = IsCompleteStringLiteral<`This can be one of ${boolean}`>
// true. `This can be one of ${boolean}` will be (`This can be one of true` | `This can be one of false`)
type TestNull = IsCompleteStringLiteral<`A pointer can point to ${null}`>
// true
type TestUndefined = IsCompleteStringLiteral<`JavaScript is ${undefined}`>
// trueRemoveTypeFromString<S extends string, T extends string | number | bigint = string | number | bigint>
Removes a type from a string literal.
type IP = RemoveTypeFromString<`1.2.3.${string}`>
// `1.2.3.`
type RemoveName = RemoveTypeFromString<`Hello, ${string}`>
// `Hello, `
type RemoveAge = RemoveTypeFromString<`${string} is ${number} years old`, number>
// `${string} is years old`Whether a type extends one of multiple types.
type test = ExtendsOneOf<string, ["hi", number]>
// true
type checkStringNumber1 = ExtendsOneOf<`${number}`, [`${number}`, `${bigint}`]>
// true
type checkStringNumber2 = ExtendsOneOf<`123`, [`${number}`, `${bigint}`]>
// true
type checkStringNumber3 = ExtendsOneOf<`12hi`, [`${number}`, `${bigint}`]>
// false
type nullable = ExtendsOneOf<string, [null, undefined]>
// falseWhether one of multiple types extends a type.
type test = OneOfExtends<["hi", number], string>
// true
type bool = OneOfExtends<[true, false], boolean>
// true
type notTrue = OneOfExtends<[string, bigint, number], boolean>
// falseWhether one of multiple types extends one of other multiple types.
type test = OneOfExtendsOneOf<["hi", number], [123n, true]>
// false
type bool = OneOfExtendsOneOf<[true, false], ["hi", boolean]>
// true
type notTrue = OneOfExtendsOneOf<[string, bigint, number], [true, false, boolean, null, undefined]>
// false