实现函数返回的传参类型控制
let fn
= ref
<null|undefined
>()
fn(null)
fn('1111')
fn(1)
interface ref_rf<U>{
(x
:U):any
;
}
function ref
<T>(): ref_rf
<T>{
return (value
:T) => {
console
.log('hello ${value}')
}
}
定义对象 & 对象数组
interface article {
[key
: string
]: {
uid
: number
,
name
: string
}
}
let a
: article
= {
'a001': {
uid
: 1,
name
:'a1'
}
}
interface article_list {
[key
:number
]: {
uid
: number
,
name
: string
}
}
or
type article_list
= Array
<article
>
let a_list
: article_list
= [
{
uid
: 1,
name
: '11'
}
]
PS: [key:number] 表示一个数组 [key:string] 表示一个对象嵌对象(毕竟数组也是一个key为自增number的对象而已)
对象内定义类型
let resume
= {
base
:{
name
:<String
>'',
age
: <Number
>0,
phone
:<String
>'',
}
}
resume
.base
.sex
= 1
or
type Tbase
= {
name
:String
age
: Number
phone
:String
,
}
let resume2
= {
base
:<Tbase
> {
name
:'',
age
: 0,
phone
:''
}
}
resume
.base
.sex
= 1
转载请注明原文地址:https://blackberry.8miu.com/read-616.html