多看下生成的JS,从而看看处理的逻辑(使用不同的ES标准进行编译)

几个迁移的案例

https://developers.google.com/web/updates/2021/01/puppeteer-typescript

ROI

  • 团队迁移成本
    (开发思维,开发生态,项目处理,接口及声明文件的维护)

redaxios

https://www.tslang.cn/docs/handbook/type-checking-javascript-files.html
https://github.com/Microsoft/TypeScript/wiki/JSDoc-support-in-JavaScript
使用JSDoc注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html
支持的JSDoc
下面的列表列出了当前所支持的JSDoc注解,你可以用它们在JavaScript文件里添加类型信息。

注意,没有在下面列出的标记(例如@async)都是还不支持的。

@type
@param (or @arg or @argument)
@returns (or @return)
@typedef
@callback
@template
@class (or @constructor)
@this
@extends (or @augments)
@enum
它们代表的意义与usejsdoc.org上面给出的通常是一致的或者是它的超集。

推荐相关文档:

https://zhuanlan.zhihu.com/p/40322215
https://labs.thisdot.co/blog/your-first-vue-3-app-using-typescript
https://medium.com/vue-typescript/vue-3-with-typescript-setup-a-new-project-with-the-vue-cli-4ea806be7a91

推荐TS入门:

https://basarat.gitbook.io/typescript/getting-started
https://jkchao.github.io/typescript-book-chinese/typings/types.html#%E4%BD%BF%E7%94%A8-types

教程推荐

项目总结

接入的第三方插件

sentry

type vs interface

官方example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// There are two main tools to declare the shape of an
// object: interfaces and type aliases.
//
// They are very similar, and for the most common cases
// act the same.

type BirdType = {
wings: 2;
};

interface BirdInterface {
wings: 2;
}

const bird1: BirdType = { wings: 2 };
const bird2: BirdInterface = { wings: 2 };

// Because TypeScript is a structural type system,
// it's possible to intermix their use too.

const bird3: BirdInterface = bird1;

// They both support extending other interfaces and types.
// Type aliases do this via intersection types, while
// interfaces have a keyword.

type Owl = { nocturnal: true } & BirdType;
type Robin = { nocturnal: false } & BirdInterface;

interface Peacock extends BirdType {
colourful: true;
flies: false;
}
interface Chicken extends BirdInterface {
colourful: false;
flies: false;
}

let owl: Owl = { wings: 2, nocturnal: true };
let chicken: Chicken = { wings: 2, colourful: false, flies: false };

// That said, we recommend you use interfaces over type
// aliases. Specifically, because you will get better error
// messages. If you hover over the following errors, you can
// see how TypeScript can provide terser and more focused
// messages when working with interfaces like Chicken.

owl = chicken;
chicken = owl;

// One major difference between type aliases vs interfaces
// are that interfaces are open and type aliases are closed.
// This means you can extend an interface by declaring it
// a second time.

interface Kitten {
purrs: boolean;
}

interface Kitten {
colour: string;
}

// In the other case a type cannot be changed outside of
// its declaration.

type Puppy = {
color: string;
};

type Puppy = {
toys: number;
};

// Depending on your goals, this difference could be a
// positive or a negative. However for publicly exposed
// types, it's a better call to make them an interface.

// One of the best resources for seeing all of the edge
// cases around types vs interfaces, this stack overflow
// thread is a good place to start:

// https://stackoverflow.com/questions/37233735/typescript-interfaces-vs-types/52682220#52682220

declare

  • https://www.typescriptlang.org/docs/handbook/declaration-files/by-example.html

    declare module ‘xxx’

  • 声明一个全局模块
  • 解决查找模块路径的问题
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    # env.d.ts

    /// <reference types="vite/client" />

    declare module '*.vue' {
    import type { DefineComponent } from 'vue'
    // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/ban-types
    const component: DefineComponent<{}, {}, any>
    export default component
    }
  • 环境声明 - *.d.ts文件, 里面每个根级别的声明需要以declare作为前缀

当你安装TypeScript时,会顺带安装一个lib.d.ts声明文件。这个文件包含JavaScript运行时及DOM(Document Object Model,文档对象模型)中存在的各种常见的JavaScript环境声明。● 它自动包含在TypeScript项目的编译上下文中。● 它能让你快速开始书写经过类型检查的JavaScript代码。

tsconfig

  • strictNullCheck 建议设置true,如果有需要都允许的地方,可以使用联合类型

sources

why

用typescript的原因,是一种思维模式上面的减负;想应该想的,一些语言特性里面的遗留问题,不需要再想

1
2
undefined == null // true 
undefined === null // false

https://github.com/lodash/lodash.git

注释

1
2
3
4
TypeScript 的 3.7 版本引入了 @ts-nocheck 注释,可以增加在 TypeScript 文件的头部来禁用语义检查。我们没有使用这个注释,因为它之前不支持.ts/.tsx 文件,但它也可以在迁移过程中成为一个很好的中间阶段助手。

TypeScript 的 3.9 版本引入了 @ts-expect-error 注释。当一行以 @ts-expect-error 注释作为前缀时,TypeScript 将禁止报告该错误。如果没有错误,TypeScript 会报告 @ts-expect-error 是不必要的。在 Airbnb 代码库,我们使用了 @ts-expect-error 而不是 @ts-ignore 。
https://medium.com/airbnb-engineering/ts-migrate-a-tool-for-migrating-to-typescript-at-scale-cd23bfeb5cc​​​