Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 类型脚本类型';未知';不可分配给类型';编号';_Javascript_Typescript - Fatal编程技术网

Javascript 类型脚本类型';未知';不可分配给类型';编号';

Javascript 类型脚本类型';未知';不可分配给类型';编号';,javascript,typescript,Javascript,Typescript,我为click handle函数编写了一个类型,该函数返回一个与其参数类型相同的值,代码如下: type OnClickHandle=(p:T扩展推断U?U:T)=> 你能推断你吗? 你的电话号码是多少?编号: 你的字符串?字符串: U扩展未定义? 无效: 无效: voidTLDR:我建议您使用以下方法 type OnClickHandle<T> = (p: T) => T; const handleReceive: OnClickHandle<number> =

我为click handle函数编写了一个类型,该函数返回一个与其参数类型相同的值,代码如下:

type OnClickHandle=(p:T扩展推断U?U:T)=>
你能推断你吗?
你的电话号码是多少?编号:
你的字符串?字符串:
U扩展未定义?
无效:
无效:

void
TLDR:我建议您使用以下方法

type OnClickHandle<T> = (p: T) => T;
const handleReceive: OnClickHandle<number> = (p: number) => p;
可以用

T extends number ? number : void
所以,如果我们不更改您的类型声明,您的实现应该如下所示

const test:Test<number> = (p:number) => p;
const test:Test = (p) => p;
test(3) // type is inferred here
const handleReceive:OnClickHandle = (p) => p; // do not declare type here, it is supposed to be generic
handleReceive(3);

我的typescript版本:3.7.2TX很多,有更多关于泛型类型和泛型函数的文章吗?比ts更新
type Test<T> = (arg: T) => T;
const test:Test<number> = (p:number) => p;
type Test = <T>(arg: T) => T;
const test:Test = (p) => p;
test(3) // type is inferred here
const handleReceive:OnClickHandle = (p) => p; // do not declare type here, it is supposed to be generic
handleReceive(3);