这里不做过多介绍 只是贴一些代码 便于理解
(注意: 下面代码都是运行在.ts 文件下的)

可以完全按照 JavaScript 标准语法编写代码

  1. // 可以完全按照 JavaScript 标准语法编写代码
  2. const hello = (name: any) => {
  3. console.log(`Hello, ${name}`)
  4. }
  5. hello('TypeScript')

原始数据类型

  1. // 原始数据类型
  2. const a: string = 'foobar'
  3. const b: number = 100 // NaN Infinity
  4. const c: boolean = true // false
  5. // 在非严格模式(strictNullChecks)下,
  6. // string, number, boolean 都可以为空
  7. // const d: string = null
  8. // const d: number = null
  9. // const d: boolean = null
  10. const e: void = undefined
  11. const f: null = null
  12. const g: undefined = undefined
  13. // Symbol 是 ES2015 标准中定义的成员,
  14. // 使用它的前提是必须确保有对应的 ES2015 标准库引用
  15. // 也就是 tsconfig.json 中的 lib 选项必须包含 ES2015
  16. const h: symbol = Symbol()
  17. // Promise
  18. // const error: string = 100

作用域问题

  1. // 作用域问题
  2. // 默认文件中的成员会作为全局成员
  3. // 多个文件中有相同成员就会出现冲突
  4. // const a = 123
  5. // 解决办法1: IIFE 提供独立作用域
  6. // (function () {
  7. // const a = 123
  8. // })()
  9. // 解决办法2: 在当前文件使用 export,也就是把当前文件变成一个模块
  10. // 模块有单独的作用域
  11. const a = 123
  12. export {}

Object 类型

  1. // Object 类型
  2. export {} // 确保跟其它示例没有成员冲突
  3. // object 类型是指除了原始类型以外的其它类型
  4. const foo: object = function () {} // [] // {}
  5. // 如果需要明确限制对象类型,则应该使用这种类型对象字面量的语法,或者是「接口」
  6. const obj: { foo: number, bar: string } = { foo: 123, bar: 'string' }
  7. // 接口的概念后续介绍

数组类型

  1. // 数组类型
  2. export {} // 确保跟其它示例没有成员冲突
  3. // 数组类型的两种表示方式
  4. const arr1: Array<number> = [1, 2, 3]
  5. const arr2: number[] = [1, 2, 3]
  6. // 案例 -----------------------
  7. // 如果是 JS,需要判断是不是每个成员都是数字
  8. // 使用 TS,类型有保障,不用添加类型判断
  9. function sum (...args: number[]) {
  10. return args.reduce((prev, current) => prev + current, 0)
  11. }
  12. sum(1, 2, 3) // => 6

元组(Tuple)

  1. // 元组(Tuple)
  2. export {} // 确保跟其它示例没有成员冲突
  3. const tuple: [number, string] = [18, 'zce']
  4. // const age = tuple[0]
  5. // const name = tuple[1]
  6. const [age, name] = tuple
  7. // ---------------------
  8. const entries: [string, number][] = Object.entries({
  9. foo: 123,
  10. bar: 456
  11. })
  12. const [key, value] = entries[0]
  13. // key => foo, value => 123

枚举(Enum)

  1. // 枚举(Enum)
  2. export {} // 确保跟其它示例没有成员冲突
  3. // 用对象模拟枚举
  4. // const PostStatus = {
  5. // Draft: 0,
  6. // Unpublished: 1,
  7. // Published: 2
  8. // }
  9. // 标准的数字枚举
  10. // enum PostStatus {
  11. // Draft = 0,
  12. // Unpublished = 1,
  13. // Published = 2
  14. // }
  15. // 数字枚举,枚举值自动基于前一个值自增
  16. // enum PostStatus {
  17. // Draft = 6,
  18. // Unpublished, // => 7
  19. // Published // => 8
  20. // }
  21. // 字符串枚举
  22. // enum PostStatus {
  23. // Draft = 'aaa',
  24. // Unpublished = 'bbb',
  25. // Published = 'ccc'
  26. // }
  27. // 常量枚举,不会侵入编译结果
  28. const enum PostStatus {
  29. Draft,
  30. Unpublished,
  31. Published
  32. }
  33. const post = {
  34. title: 'Hello TypeScript',
  35. content: 'TypeScript is a typed superset of JavaScript.',
  36. status: PostStatus.Draft // 3 // 1 // 0
  37. }
  38. // PostStatus[0] // => Draft

函数类型

  1. // 函数类型
  2. export {} // 确保跟其它示例没有成员冲突
  3. function func1 (a: number, b: number = 10, ...rest: number[]): string {
  4. return 'func1'
  5. }
  6. func1(100, 200)
  7. func1(100)
  8. func1(100, 200, 300)
  9. // -----------------------------------------
  10. const func2: (a: number, b: number) => string = function (a: number, b: number): string {
  11. return 'func2'
  12. }

任意类型(弱类型)

  1. // 任意类型(弱类型)
  2. export {} // 确保跟其它示例没有成员冲突
  3. function stringify (value: any) {
  4. return JSON.stringify(value)
  5. }
  6. stringify('string')
  7. stringify(100)
  8. stringify(true)
  9. let foo: any = 'string'
  10. foo = 100
  11. foo.bar()
  12. // any 类型是不安全的

隐式类型推断

  1. // 隐式类型推断
  2. export {} // 确保跟其它示例没有成员冲突
  3. let age = 18 // number
  4. // age = 'string'
  5. let foo
  6. foo = 100
  7. foo = 'string'
  8. // 建议为每个变量添加明确的类型标注

类型断言

  1. // 类型断言
  2. export {} // 确保跟其它示例没有成员冲突
  3. // 假定这个 nums 来自一个明确的接口
  4. const nums = [110, 120, 119, 112]
  5. const res = nums.find(i => i > 0)
  6. // const square = res * res
  7. const num1 = res as number
  8. const num2 = <number>res // JSX 下不能使用

接口

  1. // 接口
  2. export {} // 确保跟其它示例没有成员冲突
  3. interface Post {
  4. title: string
  5. content: string
  6. }
  7. function printPost (post: Post) {
  8. console.log(post.title)
  9. console.log(post.content)
  10. }
  11. printPost({
  12. title: 'Hello TypeScript',
  13. content: 'A javascript superset'
  14. })

可选成员、只读成员、动态成员

  1. // 可选成员、只读成员、动态成员
  2. export {} // 确保跟其它示例没有成员冲突
  3. // -------------------------------------------
  4. interface Post {
  5. title: string
  6. content: string
  7. subtitle?: string
  8. readonly summary: string
  9. }
  10. const hello: Post = {
  11. title: 'Hello TypeScript',
  12. content: 'A javascript superset',
  13. summary: 'A javascript'
  14. }
  15. // hello.summary = 'other'
  16. // ----------------------------------
  17. interface Cache {
  18. [prop: string]: string
  19. }
  20. const cache: Cache = {}
  21. cache.foo = 'value1'
  22. cache.bar = 'value2'

类(Class)

  1. // 类(Class)
  2. export {} // 确保跟其它示例没有成员冲突
  3. class Person {
  4. name: string // = 'init name'
  5. age: number
  6. constructor (name: string, age: number) {
  7. this.name = name
  8. this.age = age
  9. }
  10. sayHi (msg: string): void {
  11. console.log(`I am ${this.name}, ${msg}`)
  12. }
  13. }

类的访问修饰符

  1. // 类的访问修饰符
  2. export {} // 确保跟其它示例没有成员冲突
  3. class Person {
  4. public name: string // = 'init name'
  5. private age: number
  6. protected gender: boolean
  7. constructor (name: string, age: number) {
  8. this.name = name
  9. this.age = age
  10. this.gender = true
  11. }
  12. sayHi (msg: string): void {
  13. console.log(`I am ${this.name}, ${msg}`)
  14. console.log(this.age)
  15. }
  16. }
  17. class Student extends Person {
  18. private constructor (name: string, age: number) {
  19. super(name, age)
  20. console.log(this.gender)
  21. }
  22. static create (name: string, age: number) {
  23. return new Student(name, age)
  24. }
  25. }
  26. const tom = new Person('tom', 18)
  27. console.log(tom.name)
  28. // console.log(tom.age)
  29. // console.log(tom.gender)
  30. const jack = Student.create('jack', 18)

类的只读属性

  1. // 类的只读属性
  2. export {} // 确保跟其它示例没有成员冲突
  3. class Person {
  4. public name: string // = 'init name'
  5. private age: number
  6. // 只读成员
  7. protected readonly gender: boolean
  8. constructor (name: string, age: number) {
  9. this.name = name
  10. this.age = age
  11. this.gender = true
  12. }
  13. sayHi (msg: string): void {
  14. console.log(`I am ${this.name}, ${msg}`)
  15. console.log(this.age)
  16. }
  17. }
  18. const tom = new Person('tom', 18)
  19. console.log(tom.name)
  20. // tom.gender = false

类与接口

  1. // 类与接口
  2. export {} // 确保跟其它示例没有成员冲突
  3. interface Eat {
  4. eat (food: string): void
  5. }
  6. interface Run {
  7. run (distance: number): void
  8. }
  9. class Person implements Eat, Run {
  10. eat (food: string): void {
  11. console.log(`优雅的进餐: ${food}`)
  12. }
  13. run (distance: number) {
  14. console.log(`直立行走: ${distance}`)
  15. }
  16. }
  17. class Animal implements Eat, Run {
  18. eat (food: string): void {
  19. console.log(`呼噜呼噜的吃: ${food}`)
  20. }
  21. run (distance: number) {
  22. console.log(`爬行: ${distance}`)
  23. }
  24. }

抽象类

  1. // 抽象类
  2. export {} // 确保跟其它示例没有成员冲突
  3. abstract class Animal {
  4. eat (food: string): void {
  5. console.log(`呼噜呼噜的吃: ${food}`)
  6. }
  7. abstract run (distance: number): void
  8. }
  9. class Dog extends Animal {
  10. run(distance: number): void {
  11. console.log('四脚爬行', distance)
  12. }
  13. }
  14. const d = new Dog()
  15. d.eat('嗯西马')
  16. d.run(100)

泛型

  1. // 泛型
  2. export {} // 确保跟其它示例没有成员冲突
  3. function createNumberArray (length: number, value: number): number[] {
  4. const arr = Array<number>(length).fill(value)
  5. return arr
  6. }
  7. function createStringArray (length: number, value: string): string[] {
  8. const arr = Array<string>(length).fill(value)
  9. return arr
  10. }
  11. function createArray<T> (length: number, value: T): T[] {
  12. const arr = Array<T>(length).fill(value)
  13. return arr
  14. }
  15. // const res = createNumberArray(3, 100)
  16. // res => [100, 100, 100]
  17. const res = createArray<string>(3, 'foo')

类型声明

  1. // 类型声明
  2. import { camelCase } from 'lodash'
  3. import qs from 'query-string'
  4. qs.parse('?key=value&key2=value2')
  5. // declare function camelCase (input: string): string
  6. const res = camelCase('hello typed')
  7. export {} // 确保跟其它示例没有成员冲突