概念
环境
查看版本 tsc -v / npx tsc-v 查看所有版本 npm view typescript versions 安装(用于 demo)
方式1:npm install -g typescript > 编译:tsc 文件名.ts 方式2:npm i -D nodemon typescirpt ts-node(node环境下编译ts)> 初始化 tsconfig 文件:npx tsc --init
"scripts" : {
"start" : "nodemon --exec ts-node src/xx.ts"
}
类型定义
boolean、number、string、symbol
let boo : boolean
let n : number
let str : string
let s : symbol
let un = undefined
let nl = null
let un1 : undefined
let nl1 : null
const obj : { msg : string} ;
const obj1 : object = { x : 10 } ;
const obj2 : { } = { x : 10 } ;
const obj3 : Object = { } ;
const arr1 : Array< number> = [ 10 , 20 ]
const arr2 : string[ ] = [ 'a' , 'b' ]
let func : Function = ( ) => { } ;
let func2 : ( parma : string) => string
NewTypes
let func : ( ) => void
let b : void
b = undefined
unknow :未知类型:严格版的 any
区别不能进行操作、可以用于传参 所有类型可以分配给unknow、不能赋值给其他类型
let a : any
let b : unknown
a. toFixed ( 2 )
if ( typeof c === "number" ) {
b. toFixed ( 2 )
} else if ( typeof c === "object" && c instanceof Array ) {
b. join ( "-" )
}
const arr : [ ] = [ ]
function error ( ) : never {
throw new Error ( 'error ...' )
}
function loop ( ) : never {
while ( true ) {
console. log ( '死循环 ' )
}
}
其他
let x : number | string | boolean;
const arr : Array< string | number> = [ ]
let n1 = 100 ;
let obj : { num? : number }
let obj : { [ key: string] : string }
function fn ( x : string | number) {
if ( ( x as string) . length) {
return ( < string> x) . slice ( 0 , 1 )
}
}
元组和枚举
const tuple : [ string, string, string] = [ 'zs' , 'ls' , 'ww' ]
枚举:
用于定义命名常量集合的数据类型 如果没有值默认从0开始
enum Gender {
Male,
Female
}
console. log ( Gender[ 0 ] )
console. log ( Gender[ 1 ] )
console. log ( Gender. Male)
console. log ( Gender. Female)
enum Direction {
Up,
Down = 100 ,
Left,
Right
}
console. log ( Direction. Up)
console. log ( Direction. Down)
console. log ( Direction. Left)
interface
定义任意结构或者类型 相同名字的两个接口会进行合并,但是属性不能重复定义 和 type(类型别名) 的区别:type 可以定义联合类型和交叉类型
interface Point {
readonly x: number
y? : number
}
interface Point {
z : number
}
const point : Point = {
x : 1 ,
y : 2 ,
z : 3
}
interface Top {
a : ( ) => number
}
interface Bottom {
b : number
}
interface Home extends Top , Bottom { }
interface PassCheck {
name : string
age : number
}
const getTypes = ( obj : PassCheck) => { }
const options = {
name : 'zs' ,
age : 18 ,
other : '没有的属性'
}
getTypes ( options)
类
pulic - 允许外部(实例)调用 protected 允许父类和继承子类中(子类构造器或方法)使用 prevate - 只允许父类使用 继承(泛化),封装(修饰符控制外部访问权限),多态(子类可以拥有自己的方法)
class Person {
name = 'zhangsan'
protected weight = '70kg'
private phone = '110'
getInfo ( ) { }
}
class Person {
getInfo ( ) { }
}
class Girl extends Person {
getInfo ( ) { }
}
interface FoodProps {
type : string
}
class FoodClass implements FoodProps {
constructor ( public type : string) { }
}
泛型
function swapGeneric< T , U > ( tuple: [ T , U ] ) : [ U , T ] {
return [ tuple[ 1 ] , tuple[ 0 ] ]
}
console. log ( swapGeneric< number, string> ( [ 1 , 'zs' ] ) )
interface IPlus < T , U > {
( a: T , b : U ) : T
}
const ap : IPlus< number, string> = ( a: number, b : string) : number => {
return + a + + b
}
interface Ps {
length : number
}
function echoWithLength< T extends Ps > ( arg: T ) : T {
return arg
}
const strRef = echoWithLength< string> ( 'zs' )
const objRef = echoWithLength ( {
name : 'zs' ,
length : 1
} )
const arrRef = echoWithLength ( [ 1 , 2 , 3 ] )
utility
keyof :类似 Object.keys() 获取所有的键值对以联合类型定义
interface Student {
id : number
name : string
age : number
}
type keys = keyof Student
type Users = {
[ k in keys] : string
}
const x = 'zs'
type Student = typeof x
function personMan ( age : number, name : string) { }
let p : Parameters< typeof personMan>
Partial: 属性皆为可选,相当于每个属性添加?
type Person = {
name : string
job : string
age : number
}
const p1 : Partial< Person> = { name : undefined }
const o1 : Omit< Person, 'job' | 'age' > = { name : 'zs' }
const s1 : Pick< Person, 'name' > = { name : 'lisi' }
Exclude: 取出键值对作为值类型声明,第一个参数是一个联合类型
const y1 : Exclude< keyof Person, 'name' > = 'job'
as const
数组经过 typeof 会转换成相应元素类型的数组 使用 as const 强制内容必须一致
const arr = [ 1 , 2 ]
type A = typeof arr
const arrConet = [ 1 , 2 ] as const
type AC = typeof arrConet
案例
const returnPropsToObj = < K extends string > ( keys: K [ ] ) => {
let obj = { } as { [ key in K ] : string }
keys. forEach ( item => {
obj[ item] = ''
} )
return obj
}
const x = returnPropsToObj ( [ 'name' , 'id' ] )