Typescript Flashcards

1
Q

What is the Template Literal Type Syntax?

A

type attrs = “Phone” | “Name”;

type target = get${attrs};

// ✅ Result
// target = "getPhone" | "getName";
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Is valid to the next in TS?

type CustomObject = {
foo: string
}

type target = get${CustomObject}

A

No, is invalid because TS do not support objects types in string Template Literal Type Syntax.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which types could we use in Template Literal Types Syntax?

A

Only subsets of primitives like string, number, bigint, boolean, null, undefined or a union combination of those

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the String Manipulation Utilities on TS?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What the infer keyword does?

A

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;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly