Upgrade to Pro — share decks privately, control downloads, hide ads and more …

todays-typescript.pdf

 todays-typescript.pdf

Transcript

  1. interface X {! getName(): string;! getAge(): number;! }! ! class

    XImpl implements X {! getName() {return 'Aono';}! getAge() {return 31;}! }!
  2. interface M<T> {! getValue(): T;! }! ! class MImpl implements

    M<string> {! getValue(): string {! return 'foobarbaz'! }! }!
  3. type B = {! a: number,! b: string! } |

    {! a: string,! b: number! };!
  4. const x: {a: string, b: number} = obj;! const y:

    {a: number, b: string} = obj;! ! const x: {! a: MyClass<MyInnerClass<string>>,! ...! } = obj;! const y: {! a: MyClass<MyInnerClass<number>>,! ...! } = obj;!
  5. type FetchedPerson = {! [P in Person]: Promise<Person[P]>! };! !

    type FetchedPerson = {! name: Promise<string>;! age: Promise<number>;! }!
  6. type Promisify<T> = {! [P in T]: Promise<T[P]>! };! !

    type FetchedPerson = Promisify<Person>;!
  7. type MyReturnType<T> = ! T extends (...args: any) => infer

    X? X: T;! ! interface X {! getName(): string! }! ! type R = MyReturnType<X['getName']>! ꟼ侧ך䨱׶⦼׾《䖤
  8. type UnPromisifyField<T> = {! [P in keyof T]:! T[P] extends

    Promise<infer X>? UnPromisify<X>! : UnPromisify<T[P]>;! }! ! type UnPromisify<T> =! T extends Promise<infer X>? UnPromisifyField<X>! : UnPromisifyField<T>; Promise׾䒷׏⶟ָׅ
  9. type X = {! y: {! value: Promise<string>;! };! z:

    Promise<number>! }! ! type Y = UnPromisify<X>;!
  10. type ArrayElementType<T> = T extends Array<infer R>! ? R! :

    T extends ArrayLike<infer R>! ? R! : T extends NodeListOf<infer R> ? R! : T extends NodeList ? Node[] : T;! ꂁ⴨ך銲稆㘗׾《䖤
  11. type X = {! a: string;! b: number;! c: string;!

    };! ! type Y = Pick<X, 'a' | 'b'>;!