首页 > 关于typescript里的类型接口?

关于typescript里的类型接口?

interface GenericIdentityFn {
    <T>(arg: T): T;
}

function identity<T>(arg: T): T {
    return arg;
}

let myIdentity: GenericIdentityFn = identity;

新手目前正在学ts,但是一直不明白为什么接口已经定义了参数的类型,function里还要再写一遍?


你可以这么写

let myIdetity: GenericIdentityFn = (arg) => {
    return arg;
}

你在定义identity时并没表示这个函数是实现了GenericIdentityFn的,所以你得自己写一遍类型参数。
实际上function xxx() {}的写法也无法声明其实现了什么

【热门文章】
【热门文章】