ts 一个同时是函数和 属性 的类型
ts a type that is a function and a property at the same time
我正在阅读 ts 手册,发现一些非常令人困惑的东西,我不确定如何使用它。基本上是一个同时是函数和 属性 的参数。
// the definition
type DescribableFunction = {
description: string;
(someArg: number): boolean;
};
// the function that acceps the params
function doSomething(fn: DescribableFunction) {
console.log(fn.description + " returned " + fn(6));
}
// but how can I use it?
// case 1 fail
doSomething({ description: 'xxx'})
/* Argument of type '{ description: string; }' is not assignable to parameter of type 'DescribableFunction'.
Type '{ description: string; }' provides no match for the signature '(someArg: number): boolean'.(2345)
*/
//case 2 fail
doSomething((x) => x > 0})
/*
Argument of type '(x: number) => boolean' is not assignable to parameter of type 'DescribableFunction'.
Property 'description' is missing in type '(x: number) => boolean' but required in type 'DescribableFunction'.(2345)
input.tsx(2, 3): 'description' is declared here.
*/
文档
https://www.typescriptlang.org/docs/handbook/2/functions.html#call-signatures
这个代码
type DescribableFunction = {
description: string;
(someArg: number): boolean;
};
表示有一些函数(someArg: number): boolean
with static 属性 description
.
就像任何函数都有静态 属性 name
或静态方法 call
,apply
,bind
...
type DescribableFunction = {
description: string;
(someArg: number): boolean;
};
function doSomething(fn: DescribableFunction) {
console.log(fn.description + " returned " + fn(6));
}
const fn: DescribableFunction = (someArg: number) => true
fn.description = 'hello';
doSomething(fn) // ok
我正在阅读 ts 手册,发现一些非常令人困惑的东西,我不确定如何使用它。基本上是一个同时是函数和 属性 的参数。
// the definition
type DescribableFunction = {
description: string;
(someArg: number): boolean;
};
// the function that acceps the params
function doSomething(fn: DescribableFunction) {
console.log(fn.description + " returned " + fn(6));
}
// but how can I use it?
// case 1 fail
doSomething({ description: 'xxx'})
/* Argument of type '{ description: string; }' is not assignable to parameter of type 'DescribableFunction'.
Type '{ description: string; }' provides no match for the signature '(someArg: number): boolean'.(2345)
*/
//case 2 fail
doSomething((x) => x > 0})
/*
Argument of type '(x: number) => boolean' is not assignable to parameter of type 'DescribableFunction'.
Property 'description' is missing in type '(x: number) => boolean' but required in type 'DescribableFunction'.(2345)
input.tsx(2, 3): 'description' is declared here.
*/
文档 https://www.typescriptlang.org/docs/handbook/2/functions.html#call-signatures
这个代码
type DescribableFunction = {
description: string;
(someArg: number): boolean;
};
表示有一些函数(someArg: number): boolean
with static 属性 description
.
就像任何函数都有静态 属性 name
或静态方法 call
,apply
,bind
...
type DescribableFunction = {
description: string;
(someArg: number): boolean;
};
function doSomething(fn: DescribableFunction) {
console.log(fn.description + " returned " + fn(6));
}
const fn: DescribableFunction = (someArg: number) => true
fn.description = 'hello';
doSomething(fn) // ok