Typescript Flashcards
What is the Template Literal Type Syntax?
type attrs = “Phone” | “Name”;
type target = get${attrs}
;
// ✅ Result // target = "getPhone" | "getName";
Is valid to the next in TS?
type CustomObject = {
foo: string
}
type target = get${CustomObject}
No, is invalid because TS do not support objects types in string Template Literal Type Syntax.
Which types could we use in Template Literal Types Syntax?
Only subsets of primitives like string, number, bigint, boolean, null, undefined or a union combination of those
What are the String Manipulation Utilities on TS?
Uppercase: will transform the string literal to uppercase.
Lowercase: will transform the string literal to lower.
Capitalize: will uppercase the first letter.
Uncapitalize: will lowercase the first letter.
What the infer
keyword does?
This keyword allows us to deduce a Type from another within a conditional Type.
type Direction = ‘left’ | ‘right’ | ‘top’ | ‘bottom’;
type InferRoot = T extends ${infer K}${Capitalize}
? K : T;
// ✅ Result1 is 'margin' type Result1 = InferRoot;
// ✅ Result2 is 'padding' type Result2 = InferRoot;