初始化安装ts
yarn init -y
yarn add typescript -D
yarn add ts-node -D
npx tsc --init //生成 ts。config文件
不了解ts-node可以康康这里 【VSCode】使用ts-node 调试TypeScript代码 - 掘金 (juejin.cn)
01.basic.js
let userName = "Jack"
let hasLogin = true
hasLogin += ' Herrigton'
console.log(hasLogin); // 布尔值没什么用
使用ts-node 修复
把js后缀改为 ts
npx ts-node 01.basic.ts
Type 'string' is not assignable to type 'boolean'.
4 hasLogin += ' Herrigton'
学习初始化基本完成 ts的基础
let userName = "Jack"
let hasLogin: boolean = true
let myAge: number = 26
let myRegex: RegExp = /foo/
const names: string[] = userName.split(' ')
const myValues: Array<number> = [1]
interface Person {
first: string,
last: string,
cool: boolean
}
const myPerson: Person = {
first: 'yi',
last: 'gang',
cool: true
}
// record 键值对
const ids: Record<number, string> = {
10: 'a',
20: 'b',
30: 'c'
}
ids[40] = 'f'
for (let i = 0; i < 10; i++) {
console.log(i);
}
[1, 2, 3].forEach(v => console.log(v));
let arr = [4, 5, 6].map(v => v*10);
学习资料2022 TypeScript学习指南:NB闪闪8小时 TypeScript完整教程(中英字幕)_哔哩哔哩_bilibili