Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.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数组-Typescript中的一个_Javascript_Typescript_Types - Fatal编程技术网

类型应该是javascript数组-Typescript中的一个

类型应该是javascript数组-Typescript中的一个,javascript,typescript,types,Javascript,Typescript,Types,我有一个案例: const types = ['a', 'b', 'c']; const fn = (arg: *in types*) => {}; 有没有办法告诉Typescript,arg可以具有types数组中任何元素的类型 注意:我不想硬编码,例如arg:'a'|'b'|'c'是的!您必须使用作为常量调整数组的类型推断(这将类型从字符串[]更改为(“a”|“b”|“c”)[),然后您可以查找(类型的类型)[number] const types = ['a', 'b', 'c'

我有一个案例:

const types = ['a', 'b', 'c'];
const fn = (arg: *in types*) => {};
有没有办法告诉Typescript,
arg
可以具有
types
数组中任何元素的类型


注意:我不想硬编码,例如
arg:'a'|'b'|'c'
是的!您必须使用
作为常量调整数组的类型推断(这将类型从
字符串[]
更改为
(“a”|“b”|“c”)[
),然后您可以查找
(类型的类型)[number]

 const types = ['a', 'b', 'c'] as const;
 const fn = (arg: (typeof types)[number]) => {};

对!!您必须使用
作为常量调整数组的类型推断(这将类型从
字符串[]
更改为
(“a”|“b”|“c”)[
),然后您可以查找
(类型的类型)[number]

 const types = ['a', 'b', 'c'] as const;
 const fn = (arg: (typeof types)[number]) => {};

为什么不使用
any
作为类型?@AnuragSrivastava为什么使用
any
当变量可以是具体类型时?@Patrickkx为什么不使用
any
作为类型?@AnuragSrivastava为什么使用
any
当变量可以是具体类型时?@patrickx