typescript之入门篇一

2019-02-12  本文已影响0人  fanstastic

个人的理解是typescript就是es+类型检查,首先我们看一个最简单的例子

interface InnerErrorProps {
  code: number
}

const InnerError = (props: InnerErrorProps) => <h1>i am {props.code}</h1>

export const Errors = () => <InnerError code={405} />

元组:定义一个已知元素数量和元素类型的数组

let x: [string, number, any]

枚举:

enum Color = { blue, red, green }
enum Color = { blue = 2, red, green=5 }
let x: Color = Color.red
console.log(x) // 3
let x: string = Color[3]
console.log(x) // 'red'

类型断言

以下是两种类型断言的方式

let numValue: any = 'i am mana'
// 第一种
let valueLength = (<string>numValue).length
// 第二种, jsx只能用第二种
let valueLength = (numValue as string).length
上一篇 下一篇

猜你喜欢

热点阅读