TypeScript OmitThisParameter

OmitThisParameter<T>,该工具类型能够从类型T中剔除this参数类型,并构造一个新类型。在使用“Omit-ThisParameter<T>”工具类型时需要启用“--strictFunctionTypes”编译选项。示例如下:

/**
* --strictFunctionTypes=true
*/

function add(x: number): void {
   console.log(x)
}

function f0(this: object, x: number) {}
function f1(x: number) {}

// (x: number) => void
type T0 = OmitThisParameter<typeof f0>;

// (x: number) => void
type T1 = OmitThisParameter<typeof f1>;

// string
type T2 = OmitThisParameter<string>;

const x: T0 = add;
const y: T1 = add;
const z: T2 = "coolcou.com";

console.log(x)
console.log(y)
console.log(z)

输出结果:

TypeScript OmitThisParameter

酷客网相关文章:

赞(0)

评论 抢沙发

评论前必须登录!