Typescript 为什么“SomeObject[SomeEnum]”的类型从不在这里 导出枚举NativeAppEndpoint{ test=“test”, } 导出接口NativeApiEndpointContentMap{ 测试:字符串 } 类型GetContentType=T扩展了NativeApiEndpointContentMap的键?NativeApiEndpointContentMap[T]:未定义 类型GetContentTypeTest=T扩展NativeApiEndpointContentMap的键?对:错 类型a=GetContentType //a永远都不是 类型b=GetContentTypeTest //b是真的 类型c=NativeApiEndpointContentMap[NativeApiEndpoint.test] //c是字符串

Typescript 为什么“SomeObject[SomeEnum]”的类型从不在这里 导出枚举NativeAppEndpoint{ test=“test”, } 导出接口NativeApiEndpointContentMap{ 测试:字符串 } 类型GetContentType=T扩展了NativeApiEndpointContentMap的键?NativeApiEndpointContentMap[T]:未定义 类型GetContentTypeTest=T扩展NativeApiEndpointContentMap的键?对:错 类型a=GetContentType //a永远都不是 类型b=GetContentTypeTest //b是真的 类型c=NativeApiEndpointContentMap[NativeApiEndpoint.test] //c是字符串,typescript,Typescript,这里是 正如我们所看到的,b是真的,c是字符串,为什么a从来都不是问题 在您的情况下,NativeApiEndpoint contentmap不能被NativeApiEndpoint类型索引,因为它们之间没有连接 理论 枚举不是您所期望的简单字符串如果不明确定义此行为,即使字符串值相同,也不能使用枚举值为某些类型编制索引 export enum NativeApiEndpoint { test = "test", } export interface NativeApiE

这里是

正如我们所看到的,b是真的,c是字符串,为什么a从来都不是问题 在您的情况下,
NativeApiEndpoint contentmap
不能被
NativeApiEndpoint
类型索引,因为它们之间没有连接

理论 枚举不是您所期望的简单字符串如果不明确定义此行为,即使字符串值相同,也不能使用枚举值为某些类型编制索引

export enum NativeApiEndpoint {
  test = "test",
}
export interface NativeApiEndpointContentMap {
  test: string
}
type GetContentType<T extends NativeApiEndpoint> = T extends keyof NativeApiEndpointContentMap ? NativeApiEndpointContentMap[T] : undefined
type GetContentTypeTest<T extends NativeApiEndpoint> = T extends keyof NativeApiEndpointContentMap ? true : false
type a = GetContentType<NativeApiEndpoint.test>
// a is never
type b = GetContentTypeTest<NativeApiEndpoint.test>
// b is true
type c = NativeApiEndpointContentMap[NativeApiEndpoint.test]
// c is string

相关:谢谢!这就是我要找的!有时候打字稿对我来说太混乱了。
export enum NativeApiEndpoint {
  test = "test",
  some = 'some',
  other = 'other'
}

// false
type check = NativeApiEndpoint extends 'test' ? true : false;

// also false
type check2 = 'test' extends NativeApiEndpoint ? true : false;

// true
type check3 = NativeApiEndpoint.test extends NativeApiEndpoint ? true : false;

// true
type check4 = NativeApiEndpoint.test extends 'test' ? true : false;

// false
type check5 = 'test' extends NativeApiEndpoint.test ? true : false;

type CheckContentType<T extends NativeApiEndpoint> = T extends keyof NativeApiEndpointContentMap
  ? T extends never
    ? 'T is never'
    : 'Works'
  : undefined

// en is of type NativeApiEndpoint, not of type 'test' as you would expect
type en = NativeApiEndpoint.TEST
// checkType is of type `T is never`
type checkType = CheckContentType<en>
export enum NativeApiEndpoint {
  test = "test",
  some = 'some',
  other = 'other'
}

export interface NativeApiEndpointContentMap {
  [NativeApiEndpoint.test]: string;
  [NativeApiEndpoint.some]: number;
}

type GetContentType<T extends NativeApiEndpoint> = T extends keyof NativeApiEndpointContentMap ? NativeApiEndpointContentMap[T] : undefined

// some is number
type some = GetContentType<NativeApiEndpoint.some>
// which equals
type somee1 = NativeApiEndpointContentMap[NativeApiEndpoint.some]

// test is string
type test = GetContentType<NativeApiEndpoint.test>
// which equals
type test1 = NativeApiEndpointContentMap[NativeApiEndpoint.test]

// other is undefined, because it's key is not defined on `NativeApiEndpointContentMap`
type other = GetContentType<NativeApiEndpoint.other>
export enum NativeApiEndpoint {
  test = "test",
  some = 'some',
  other = 'other'
}

export interface NativeApiEndpointContentMap {
  test: string;
  some: number;
  problem: 'problem';
}

type GetContentType<T extends keyof NativeApiEndpointContentMap> = T extends NativeApiEndpoint
  ? NativeApiEndpointContentMap[T]
  : undefined

// some is number
type some = GetContentType<NativeApiEndpoint.some>
// which equals, but in this case GetContentType also adds constraint for passed parameter to be of `NativeApiEndpoint` type
type some1 = NativeApiEndpointContentMap[NativeApiEndpoint.some]

// could work, but 'problem' is not of type NativeApiEndpoint, so following type is undefined
type porblem = GetContentType<'problem'>

// errors, beacause `NativeApiEndpointContentMap` can not be indexed with 'other'
type other = GetContentType<NativeApiEndpoint.other>
type other1 = NativeApiEndpointContentMap[NativeApiEndpoint.other]