Javascript Typescript-有没有办法通过常量将类型应用于变量?

Javascript Typescript-有没有办法通过常量将类型应用于变量?,javascript,typescript,Javascript,Typescript,这是我所期望的(这不是有效的TS): const type1=“number”; 设myVariable1:typeof=12; 将type2=“string”设为常量; 让myVariable2:typeof=“foo”; 有没有办法在TypeScript中应用这样的类型?您可以使用: type typeByString=T扩展了“number”?数字:T扩展“字符串”?字符串:任意; 类型str=typeByString;//str是字符串 type num=typeByString;//

这是我所期望的(这不是有效的TS):

const type1=“number”;
设myVariable1:typeof=12;
将type2=“string”设为常量;
让myVariable2:typeof=“foo”;
有没有办法在TypeScript中应用这样的类型?

您可以使用:

type typeByString=T扩展了“number”?数字:T扩展“字符串”?字符串:任意;
类型str=typeByString;//str是字符串
type num=typeByString;//num是数字
您可以使用:

type typeByString=T扩展了“number”?数字:T扩展“字符串”?字符串:任意;
类型str=typeByString;//str是字符串
type num=typeByString;//num是数字

这是受塞巴斯蒂安·斯佩特尔(Sebastian Speitel)回答的启发:

我们还可以对个人类型使用类型检查:

type Person = { name : string, age : number };

type typeByString<T> = T extends "Person" ? Person : number;

const a: typeByString<"Person"> = {name : "Foo", age : 20};
const b: typeByString<"other"> = 23;
type Person={name:string,age:number};
类型typeByString=T扩展了“Person”?人:号码;;
常量a:typeByString={name:“Foo”,年龄:20};
常数b:typeByString=23;
还可以从字符串数组中选择字符串类型:

type typeByString<T> = T extends 'number' ? number : T extends 'string' ? string : T extends 'boolean' ? boolean : T extends 'null' ? null : T extends 'number[]' ? number[] : T extends 'string[]' ? string[] : T extends 'boolean[]' ? boolean[] : any;

const myTypes = ["string", "null"]  as const;
const selected = myTypes[0];
const c : typeByString<typeof selected> = "cool";
const d : typeByString<typeof myTypes[1]> = null;
type typeByString=T扩展“number”?编号:T扩展“字符串”?字符串:T扩展了“布尔值”?布尔值:T扩展“null”?null:T扩展了“number[]”吗?编号[]:T扩展“字符串[]”?字符串[]:T扩展了“布尔[]”?布尔[]:任意;
常量myTypes=[“字符串”,“空”]作为常量;
const selected=myTypes[0];
常数c:typeByString=“cool”;
常量d:typeByString=null;

这是受塞巴斯蒂安·斯佩特尔(Sebastian Speitel)回答的启发:

我们还可以对个人类型使用类型检查:

type Person = { name : string, age : number };

type typeByString<T> = T extends "Person" ? Person : number;

const a: typeByString<"Person"> = {name : "Foo", age : 20};
const b: typeByString<"other"> = 23;
type Person={name:string,age:number};
类型typeByString=T扩展了“Person”?人:号码;;
常量a:typeByString={name:“Foo”,年龄:20};
常数b:typeByString=23;
还可以从字符串数组中选择字符串类型:

type typeByString<T> = T extends 'number' ? number : T extends 'string' ? string : T extends 'boolean' ? boolean : T extends 'null' ? null : T extends 'number[]' ? number[] : T extends 'string[]' ? string[] : T extends 'boolean[]' ? boolean[] : any;

const myTypes = ["string", "null"]  as const;
const selected = myTypes[0];
const c : typeByString<typeof selected> = "cool";
const d : typeByString<typeof myTypes[1]> = null;
type typeByString=T扩展“number”?编号:T扩展“字符串”?字符串:T扩展了“布尔值”?布尔值:T扩展“null”?null:T扩展了“number[]”吗?编号[]:T扩展“字符串[]”?字符串[]:T扩展了“布尔[]”?布尔[]:任意;
常量myTypes=[“字符串”,“空”]作为常量;
const selected=myTypes[0];
常数c:typeByString=“cool”;
常量d:typeByString=null;

我认为支持这一点的最简单方法是创建一个将类型名称映射到实际类型的接口。然后,您只需查找它们:

interface LookupType {
  number: number
  string: string
  boolean: boolean
  "number[]": number[]
  person: Person
}

const type1 = "number";
let myVariable1: LookupType[typeof type1] = 12;

let type2 = "string" as const;
let myVariable2: LookupType[typeof type2] = "foo";

let type3 = 'number[]' as const
let myVariable3: LookupType[typeof type3] = [1,2,3]
这是因为该接口的键是字符串,即使该字符串恰好与其引用的类型同名。这将是一个比一组分支条件更容易维护的列表


它还有一个额外的好处,就是可以轻松地检查不受支持的类型

let doesntExist = 'badTypeName' as const
let badVar: LookupType[typeof doesntExist] = 123
// Property 'badTypeName' does not exist on type 'LookupType'.(2339)

我认为支持这一点的最简单方法是创建一个接口,将您的类型名称映射到实际类型。然后,您只需查找它们:

interface LookupType {
  number: number
  string: string
  boolean: boolean
  "number[]": number[]
  person: Person
}

const type1 = "number";
let myVariable1: LookupType[typeof type1] = 12;

let type2 = "string" as const;
let myVariable2: LookupType[typeof type2] = "foo";

let type3 = 'number[]' as const
let myVariable3: LookupType[typeof type3] = [1,2,3]
这是因为该接口的键是字符串,即使该字符串恰好与其引用的类型同名。这将是一个比一组分支条件更容易维护的列表


它还有一个额外的好处,就是可以轻松地检查不受支持的类型

let doesntExist = 'badTypeName' as const
let badVar: LookupType[typeof doesntExist] = 123
// Property 'badTypeName' does not exist on type 'LookupType'.(2339)

谢谢您的回复。但是让我们想象一下,我想添加一个布尔值和一个空类型检查,这个技术对所有类型都有效吗?好的,我理解,我们必须以一种详尽的方式列出所有类型是的。这只适用于标准类型之外的其他类型你说的“仅适用于标准类型”是什么意思?你可以检查任何其他类型并返回任何其他类型。例如,
T扩展了“map”?地图:任何谢谢你的回复。但是让我们想象一下,我想添加一个布尔值和一个空类型检查,这个技术对所有类型都有效吗?好的,我理解,我们必须以一种详尽的方式列出所有类型是的。这只适用于标准类型之外的其他类型你说的“仅适用于标准类型”是什么意思?你可以检查任何其他类型并返回任何其他类型。例如,
T扩展了“map”?地图:任何