Typescript 使用“时设置键类型”;输入ObjectName“;
我正在尝试设置对象的键类型,如下所示:Typescript 使用“时设置键类型”;输入ObjectName“;,typescript,Typescript,我正在尝试设置对象的键类型,如下所示: type TypeSample = { [key: string]: string } enum EnumSample { 'ok' = '200', } type TypeSample = { [key in EnumSample]: string } 同时还指定键来自如下枚举: type TypeSample = { [key: string]: string } enum EnumSample { 'o
type TypeSample = {
[key: string]: string
}
enum EnumSample {
'ok' = '200',
}
type TypeSample = {
[key in EnumSample]: string
}
同时还指定键来自如下枚举:
type TypeSample = {
[key: string]: string
}
enum EnumSample {
'ok' = '200',
}
type TypeSample = {
[key in EnumSample]: string
}
不设置键类型会导致错误元素隐式地具有“any”类型,因为类型为“string”的表达式不能用于索引类型为“TypeSample”
以显示在我的“getter”函数上
我的问题:我如何修改“TypeSample”让TypeScript知道它只有字符串键
注意:对于代码的其他部分来说,“EnumSample”实际上是一个枚举,而不是一个类型/接口,这一点很重要
背景: 下面是我正在使用的代码的浓缩版本:
enum SupportedHttpStatuses {
'ok' = '200',
'badRequest' = '400',
'imATeapotSafe' = '418a',
'imATeapot' = '418b',
}
type StatusMapType = {
[key in SupportedHttpStatuses]: StatusType // I want to set the key type here
}
type StatusType = {
code: number, // status code to send to the browser
title: string, // error title
description: string // error description
}
class Test {
public static STATUS_MAP: StatusMapType = {
'200': {
code: 200,
title: 'OK',
description: 'This request has succeeded.',
},
'400': {
code: 400,
title: 'Bad Request',
description: 'This request is missing data or contains invalid information.',
},
'418a': {
code: 200,
title: 'I\'m A Teapot!',
description: 'This request was successful but it is april fools day.',
},
'418b': {
code: 418,
title: 'I\'m A Teapot!',
description: 'This request was successful but it is april fools day.',
},
}
public static async getStatusMap(statusId: string): Promise<StatusType> {
return this.STATUS_MAP[statusId] // Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'StatusMapType'
}
}
枚举支持的HttpStatus{
“ok”=“200”,
“badRequest”=“400”,
'imATeapotSafe'=“418a”,
'imATeapot'='418b',
}
类型StatusMapType={
[输入SupportedHttpStatuses]:状态类型//我想在这里设置键类型
}
类型StatusType={
代码:number,//要发送到浏览器的状态代码
title:string,//错误标题
description:string//error description
}
课堂测试{
公共静态状态映射:StatusMapType={
'200': {
代码:200,,
标题:“好的”,
描述:“此请求已成功。”,
},
'400': {
代码:400,
标题:“错误请求”,
描述:“此请求缺少数据或包含无效信息。”,
},
“418a”:{
代码:200,,
标题:“我是一个茶壶!”,
描述:“此请求已成功,但今天是愚人节。”,
},
“418b”:{
代码:418,
标题:“我是一个茶壶!”,
描述:“此请求已成功,但今天是愚人节。”,
},
}
公共静态异步getStatusMap(statusId:string):承诺{
返回此值。STATUS\u MAP[statusId]//元素隐式具有“any”类型,因为类型为“string”的表达式不能用于索引类型为“StatusMapType”
}
}
我建议您更改
getStatusMap(statusId:string):Promise
到
getStatusMap(状态ID:SupportedHttpStatuses):承诺
这个.STATUS\u映射[statusId]将知道statusId是一个受支持的HttpStatuses,因此当STATUS\u映射被索引时将返回一个StatusType我没有解决方案,但每次我在typescript中使用枚举时都会很痛苦。我不知道我在哪里读到的,但我记得读到,
enum
是一个非常早期的功能,可能不会在今天的typescript中使用,因为它们违反了一些设计规则。我尽量避免它们。也许我遗漏了什么,但我认为您只需要在getStatusMap中将statusId:string更改为statusId:SupportedHttpStatuses。@Countingstuff Welp间接解决了我的问题。加上这个答案,你至少会得到一张赞成票。从技术上讲,它不能回答实际的问题,所以我想在我将你的评论标记为实际答案之前,给其他人一个回答的机会。我觉得这个评论似乎是正确的答案。编译器知道所有的有效键都是字符串,但它并不认为所有的字符串都是有效键,因为您告诉它这些键只会是该枚举中的字符串。使用字符串
对该对象进行索引是不安全的,因为它可能是某个对象中不存在的随机字符串。你认为实际的答案是什么?”我想得越多,你的答案就越正确。如果你加上你的答案,那么我会把它标记为正确答案。