Typescript-字符串连接为类型
在将文字联合转换为类型名称时,是否可以进行一些基本的字符串操作 例如:Typescript-字符串连接为类型,typescript,Typescript,在将文字联合转换为类型名称时,是否可以进行一些基本的字符串操作 例如: type Strings = "Fork" | "Spoon"; type GetMethods = { // I want this to be automatically generated basing on Strings getFork: Function, getSpoon: Function } 在TypeScript v.4.1中,您可以使用它非常轻松地
type Strings = "Fork" | "Spoon";
type GetMethods = { // I want this to be automatically generated basing on Strings
getFork: Function,
getSpoon: Function
}
在TypeScript v.4.1中,您可以使用它非常轻松地执行此操作:
type Strings = "Fork" | "Spoon";
type FuncNames = `get${Strings}`;
type GetMethods = { [K in FuncNames]: Function }
在TS4.1之前,这是可能的