chevron_left

Type Script 教程

免费
第 1.2 回

教程文档

Type Script 教程 - TypeScript 基础语法

2024年04月27日
Type Script 教程

TypeScript 基础语法

类型推断

let str = 'hello'
str = 100

自动推断为字符串,赋值为数字就报错

Type 'number' is not assignable to type 'string'.

类型注解

let str: string = 'hello'

let str2: string
str2 = 'world'

类型断言

let num = [1, 2, 3]
const res = num.find(item => item > 2)
console.log(res * 100)

会提示:

'res' is possibly 'undefined'.

可以使用as断言,find执行后的结果一定是数字

const res = num.find(item => item > 2) as number

TS 基础类型

let v1: string = 'hello'
le...

课程介绍