声明类型
type Name = string
const str: Name = 'frank'
将string属性给Name str属于Name类型,也就有string属性
type Id = string | number
const id1:Id = '123'
const id1:Id = 123
既有string属性,又有number属性
type A = {
t:'a';
specialForA:string
}
type B = {
tt:'b';
specialForb:number
}
type Target1 = A | B
const c1:Target1 ={
t:'a',
specialForA:'123'
}
c1的属性整体要么A,要么B,前提要有相同的t,
同名属性内容没法重合,就不能用&
type A = {
ta:'a';
specialForA:string
}
type B = {
tb:'b';
specialForb:number
}
type Target1 = A & B
const c1:Target1 ={
ta:'a',
tb:'b',
specialForA:'123',
specialForb:123
}
type Dog = string
type Cat = 'Cat'
interface User {
readonly id:number | string
name:string
age?: number
}
interface UserWithDog extends User{
dog:Dog
}
const u1:UserWithDog={
name: 'jack',
id:1,
dog:'hi'
}
interface UserWithPet<T> extends User{
pet:T
}
const u2:UserWithPet<Cat> = {
id:1,
name:'tt',
age:3,
pet:'Cat'
}
interface只能用来写对象,