TS

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

How do you use a template type (A)?

A<number>

What is another term for freshness

strict object literal checking

How do you add a discriminatory member to a type? E.g., Shape = Square | Rect. How can you hint to the compiler that shape is Rect? What is this called?

Have a type of a string literal, and then check for it. {type: "rect"} -> if (shape.type === 'rect'). It will be automatically assigned to the object when created, and the compiler can figure out which. It's called a discriminated union.

How do you allow all excess properties on object literals for structure typed arguments?

Include an index signature: type T = {foo: number; [x: string]: any}

How can you create versioning types?

Make the type a union of all version structures, and have future versions have a member 'version' with type as an integer number literal. Then you can use union discrimination

How do you allow specific excess properties on object literals for structure typed arguments?

Make them optional in the structure signature. type T = {foo: number, extra?: boolean}

What is freshness?

Makes it easier to type check object literals that would otherwise be structurally type compatible. If it's fresh / object literal, then it is type checked more strictly.

function test(a: number): void; function test(a: string): void; function test(a: any): void {...} test(true); Is this valid?

No because of the overload declarations.

function test(a: {name: string}): void {} const target: {name: string} = {name: '', age: 5}; Is this valid?

No. Object literals require exact matches of structure.

How are objects and object literals handled differently for structurally typed parameters?

Object literals must match the type exactly. Non literals can have excess members.

What template type is available to make all members readonly?

Readonly<T>

How can you handle the function reaching unreachable code?

Return a never object while throwing. The compiler will see that never is being returned, and runtime, the exception will be thrown.

What is structural typing?

The member structure of objects: {name: string;}

Can index signatures and optional argument structures be used together?

Yes

const fromCopy = {name: "", age: 5}; const target: {name: string} = fromCopy Is this valid?

Yes. Non object literals allow excess members.

How do you specify the signature of object indexing (index signature)?

[x: string]: Type

How do you assert `a` is `Type`?

a as Type

How do you assert a type that the compiler can't do? E.g., string to number. What is it called?

a as any as number, a double assertion

How do you implement interface in class?

class Class implements Interface {}

How do you declare a as number (implicit w/ assert, explicit w/ assert, implicit w/o assert, explicit w/o assert)?

const a = 5 as number; const a: number = 5 as number; const a = 5; const a: number = 5;

How do you specify a callback's type as template inline?

const a = <T>(a: T): T => a;

How do you assign object aa to variable bb with interface Inter?

const aa: Inter = bb; const aa = bb as Inter;

How do you create an enum?

enum { A, B, C };

How do you create an enum with values 1, 2, 3?

enum {A=1, B, C};

How do you create an enum with values 1, 3, 5?

enum {A=1, B=3, C=5};

How do you declare an interface?

interface Inter { aa: number; bb: number; }

Create a fail function that returns never.

function fail(msg: string): never {throw msg}

How do you specify no return value?

function test(): void {}

How do you specify a default value for optional arg?

function test(a: number = 5): void {}

How do you declare function overloads with changing parameters types? (e.g., test(1) and test("hello", "world") but not test(1, "world"))

function test(a: number): number; function test(a: string, b: string): string; function test(a: any, b?: string): any { if (b === undefined) { a = a as number } else { a = a as string } }

How do you specify optional arguments (2 ways)?

function test(a?: number): void {} function test(a: number = 5): void {}

How do you specify a callback's type inline?

function test(cb: (a: T1) => T2): void {}

How do you declare function overloads?

function test(val1: number, val2: number); function test(val1: number, val2: number, val3: number); function test(aa: number, bb: number, cc?: number) {...}

How do you declare a templated function

function test<T>(aa: T, bb: T): void {}

How do you make a property of an interface readonly?

interface Inter { readonly prop: string }

If a function doesn't return (e.g., throws something), what's its type?

never

How do you specify that a member is an array of type number?

number[]

How do you declare a type?

type A = B;

How do you limit generic object indexing to specific indexes?

type Index = 'a' | 'b' | 'c' type FromIndex = { [k in Index]?: number }

How do you declare a tuple?

type MyTuple = [Type1, Type2, Type3, ...]

How do you declare a templated type?

type TheType<T> = { aa: T; bb: T; }

How do you create a callback type? (short/inline, with object literal, object literal with overloads)

type short = (a: number) => number; type Obj = { (a: number): number; } type OverObj = { (a: number): number; (a: string): string; }


संबंधित स्टडी सेट्स

FIN 320: Chapter Eight (Risk and Rates of Return)

View Set

Celé čísla opakovanie, na 1. písomnú prácu

View Set

Chapter 40: Management of Patients with Gastric and Duodenal Disorders

View Set

Sir Gawain and the Green Knight Quiz

View Set

Biology: Chapter 4 Practice Test

View Set

Ch. 2 The Personal Training Profession

View Set

Chapter 10 - Conflict and Negotiation

View Set

Psychology - Lifespan Developement

View Set