Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

精读《Typescript2.0 - 2.9》 #85

Closed
ascoders opened this issue May 24, 2018 · 4 comments
Closed

精读《Typescript2.0 - 2.9》 #85

ascoders opened this issue May 24, 2018 · 4 comments

Comments

@ascoders
Copy link
Owner

精读原文是 typescript 2.0-2.9 的文档。

其中 2.0 ~ 2.8 的地址:http://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html
2.9 草案地址:https://blogs.msdn.microsoft.com/typescript/2018/05/16/announcing-typescript-2-9-rc/

通过长期工作,我意识到深入掌握 typescript 需要刻意练习,即便 2.0-2.9 的新特征可以优雅解决许多业务问题,但我们不了解也就并不会用,更糟糕的是 typescript 的深入语法是无法从业务代码中锻炼的,因为我们总是能忍受糟糕的业务代码,甚至是不知道原来代码可以优化。

只有先了解它,才能更“聪明”的解决问题,这周我们一起刻意练习 typescript 吧!

@crimx
Copy link

crimx commented May 24, 2018

2.8 解决了很多痛点,收集了几个相关的 recipes 分享一下

  1. Deep Readonly

    interface DeepReadonlyArray<T> extends ReadonlyArray<DeepReadonly<T>> {}
    
    type DeepReadonlyObject<T> = {
        readonly [P in keyof T]: DeepReadonly<T[P]>
    }
    
    type DeepReadonly<T> =
        T extends (infer R)[]
          ? DeepReadonlyArray<R>
          : T extends Function
            ? T
            : T extends object
              ? DeepReadonlyObject<T>
              : T
  2. Mutable

    type Mutable<T> = { -readonly [P in keyof T]: T[P] }
  3. 利用 ReturnType 直接拿 type 可以减少 boilerplate

还有两个很常见但一直没有被收进的

  1. Diff
    type Diff<T extends string, U extends string> = ({[P in T]: P } & {[P in U]: never } & { [x: string]: never })[T]
  2. Omit
    type Omit<T, K extends keyof T> = Pick<T, Diff<keyof T, K>>

@atian25
Copy link

atian25 commented May 24, 2018

2.8 新出的一些新特性,才使得我们的 Egg 的 TS 化完美了不少,whxaxes/blog#12

@ascoders
Copy link
Owner Author

ascoders commented May 27, 2018

@crimx TS 2.8 内置了 Exclude 作为官方版的 Diff

@crimx
Copy link

crimx commented May 27, 2018

@ascoders 看到了!谢谢提醒

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants