Typescript Flashcards

(52 cards)

1
Q

Type annotation

A

Explicitly writing a type for a variable/param/return. Example: let x: number = 3;

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

Type inference

A

TypeScript infers a type when you don’t annotate. Example: let x = 3; // x: number

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

Structural typing

A

Compatibility is based on shape (members), not “named” types. Example: {id:string} is assignable to any type requiring id:string

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

Assignability

A

Whether a value of type A can be used where type B is expected (A “assignable to” B). Example: “hi” assignable to string; string not assignable to “hi”

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

Literal type

A

A type that is a specific value. Example: “open” (not string), 1 (not number), true (not boolean).

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

Widening

A

When a literal becomes a broader primitive type in inference. Example: let s = “a”; // s: string (widened from “a”)

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

Const assertion (as const)

A

Freezes values to literal types + readonly. Example: const a = [“x”, 1] as const; // readonly [“x”, 1]

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

Type assertion (cast)

A

Telling TS “treat this as X” without runtime conversion. Example: value as SomeType

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

satisfies operator

A

Checks a value matches a type without changing the value’s inferred type. Example: const x = {a:1} satisfies Record<string, number>

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

any

A

Opt-out of type checking; anything goes. Example: let x: any = 1; x.foo.bar(); // allowed

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

unknown

A

Top type you must narrow before use. Example: let x: unknown; x.toFixed() // error until narrowed

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

never

A

Type of values that never occur (impossible) or functions that never return. Example: function fail(): never { throw new Error() }

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

void

A

Function returns nothing meaningful. Example: function log(): void { console.log(“hi”) }

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

Union type

A

Either/or type. Example: string | number

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

Intersection type

A

Combine requirements of both types. Example: A & B requires all members from A and B

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

Optional property

A

Property may be missing. Example: type U = { flags?: {admin:boolean} } // flags can be absent

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

Nullable / undefined union

A

When a type explicitly includes null/undefined. Example: {admin:boolean} | undefined

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

Narrowing

A

Refining a broader type to a more specific type along a control-flow path. Example: if (typeof x === “string”) { x.toUpperCase() }

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

Type guard

A

A runtime check TS recognizes to narrow types. Example: typeof x === “string”, x instanceof Date, “prop” in obj

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

User-defined type guard

A

A function returning a predicate like x is T. Example: function isFoo(x:any): x is Foo { return “foo” in x }

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

Discriminated union (tagged union)

A

Union of objects with a common literal “tag” property used for narrowing. Example: {kind:”a”,…}|{kind:”b”,…}

22
Q

Control-flow analysis

A

TS tracks possible execution paths to compute narrowed types in each branch. Example: after if (x) { /* x non-null */ }

23
Q

keyof operator

A

Produces a union of property names. Example: type K = keyof User // “id” | “name” | “flags”

24
Q

Indexed access type

A

Access a property’s type via indexing. Example: type Name = User[“name”]

25
Generic
Types parameterized by other types (“type variables”). Example: Array, function id(x:T):T
26
Type parameter
The variable in a generic. Example: function f(x:T){} // T is a type parameter
27
Type argument
The specific type you plug into a generic. Example: f("hi") // string is a type argument
28
Type parameter inference
TS infers type arguments from values. Example: id("hi") infers T=string
29
Generic constraint
Restrict a type parameter with extends. Example: function len(x:T){ return x.length }
30
Default type parameter
A fallback if caller doesn’t provide a type argument. Example: type Box = {value:T}
31
Generic “relationship”
Generics often express that two things share a type. Example: function pair(a:T,b:T):[T,T] keeps them the same T
32
Overload signatures
Multiple call signatures for one function (often in libs). Example: function f(x:string):string; function f(x:number):number;
33
Call signature
A function type written as a signature. Example: type Fn = (x:number) => string
34
Conditional type
Type-level if/else using extends. Example: T extends string ? "S" : "N"
35
infer keyword
Within conditional types, captures a type part into a new type variable. Example: T extends (infer U)[] ? U : T
36
Distributive conditional type
If the checked type is a naked type param, condition applies to each union member. Example: (A|B) extends ... becomes (A extends ...)|(B extends ...)
37
Mapped type
Create a new object type by iterating keys. Example: type ReadonlyT = { [K in keyof T]: T[K] }
38
Mapped type modifiers
Add/remove readonly/optional. Example: { [K in keyof T]-?: T[K] } removes optional
39
Utility types (built-in)
Standard helpers like Partial, Pick, Omit, ReturnType, Parameters, Awaited, etc.
40
Pick
Select a subset of properties. Example: Pick
41
Omit
Remove properties. Example: Omit
42
Partial
Make all props optional. Example: Partial
43
Required
Make all props required. Example: Required
44
NonNullable
Remove null and undefined from a type. Example: NonNullable gives {admin:boolean}
45
ReturnType
Extract function return type. Example: ReturnType<() => number> is number
46
Parameters
Extract function parameter tuple. Example: Parameters<(a:string,b:number)=>void> is [string, number]
47
Awaited
Recursively unwrap Promise-like types. Example: Awaited> is string
48
Template literal type
Build string types from pieces. Example: type Event = `on${Capitalize}`
49
Declaration file (.d.ts)
Type-only file describing JS APIs. Often what you read when deciphering library types.
50
Module augmentation
Add/merge types into an existing module’s declarations. Example: declare module "lib" { interface Options { ... } }
51
Declaration merging
Certain declarations with the same name merge (interfaces, namespaces, etc.). Example: interface X {a:number} interface X {b:number} => X has a & b
52
DefinitelyTyped (@types/…)
Community repo that provides typings for JS libraries via @types packages. Example: @types/lodash