[英]How to pick or access Indexer/Index signature property in a existing type in Typescript
Edit: Changed title to reflect the problem properly.编辑:更改标题以正确反映问题。
I am trying to pick the exact type definition of a specific property inside a interface, but the property is a mapped type [key: string]:
.我试图在接口内选择特定属性的确切类型定义,但该属性是映射类型
[key: string]:
。 I tried accessing it using T[keyof T]
because it is the only property inside that type but it returns never
type instead.我尝试使用
T[keyof T]
访问它,因为它是该类型中唯一的属性,但它never
返回类型。
is there a way to like Pick<Interface, [key: string]>
or Interface[[key: string]]
to extract the type?有没有办法喜欢
Pick<Interface, [key: string]>
或Interface[[key: string]]
来提取类型?
The interface I am trying to access is type { AWS } from '@serverless/typescript';
我试图访问的接口是
type { AWS } from '@serverless/typescript';
export interface AWS { configValidationMode?: "error" | "warn" | "off"; deprecationNotificationMode?: "error" | "warn" | "warn:summary"; disabledDeprecations?: "*" | ErrorCode[]; frameworkVersion?: string; functions?: { [k: string]: { // <--- Trying to pick this property. name?: string; events?: ( | { __schemaWorkaround__: null; } | { schedule: | string | { rate: string[]; enabled?: boolean; name?: string; description?: string; input?: | string /// Didn't include all too long..
You can use indexed access types here.您可以在此处使用索引访问类型。 If you have an object-like type
T
and a key-like type K
which is a valid key type for T
, then T[K]
is the type of the value at that key.如果你有一个类对象类型
T
和一个类键类型K
,它是T
的有效键类型,那么T[K]
就是该键值的类型。 In other words, if you have a value t
of type T
and a value k
of type K
, then t[k]
has the type T[K]
.换句话说,如果您有一个类型为
T
的值t
和一个类型为K
的值k
,则t[k]
的类型为T[K]
。
So the first step here is to get the type of the functions
property from the AWS
type:所以这里的第一步是从
AWS
类型中获取functions
属性的类型:
type Funcs = AWS["functions"];
/* type Funcs = {
[k: string]: {
name?: string | undefined;
events?: {
__schemaWorkaround__: null;
} | {
schedule: string | {
rate: string[];
enabled?: boolean;
name?: string;
description?: string;
input?: string;
};
} | undefined;
};
} | undefined */
Here AWS
corresponds to the T
in T[K]
, and the string
literal type "functions"
corresponds to the K
type.这里
AWS
对应的是T
T[K]
中的T, string
字面量类型"functions"
对应的是K
类型。
Because functions
is an optional property of AWS
, the Funcs
type is a union of the declared type of that property with undefined
.因为
functions
是AWS
的可选属性,所以Funcs
类型是该属性的声明类型与undefined
的联合。 That's because if you have a value aws
of type AWS
, then aws.functions
might be undefined
.那是因为如果您有一个
aws
类型的值AWS
,那么aws.functions
可能是undefined
。 You can't index into a possibly undefined
value safely, so the compiler won't let you use an indexed access to type to drill down into Funcs
directly.您无法安全地索引到可能
undefined
的值,因此编译器不会让您使用索引访问类型直接深入到Funcs
。 Something like Funcs[string]
will be an error. Funcs[string]
之类的东西将是一个错误。
So first we need to remove filter out the undefined
type from Functions
.所以首先我们需要从
Functions
中删除过滤掉undefined
的类型。 The easiest way to do this is with the NonNullable<T>
utility type which filters out null
and undefined
from a union type T
:最简单的方法是使用
NonNullable<T>
实用程序类型,它从联合类型T
中过滤掉null
和undefined
:
type DefinedFuncs = NonNullable<Funcs>;
/* type DefinedFuncs = {
[k: string]: {
name?: string | undefined;
events?: {
__schemaWorkaround__: null;
} | {
schedule: string | {
rate: string[];
enabled?: boolean;
name?: string;
description?: string;
input?: string;
};
} | undefined;
};
} */
Okay, now we have a defined type with a string
index signature whose property type is the type we're looking for.好的,现在我们有了一个带有
string
索引签名的已定义类型,其属性类型就是我们要查找的类型。 Since any string
-valued key can be used to get the property we're looking for, we can use an indexed access type with DefinedFuncs
as the object type and string
as the key type:由于任何
string
值键都可用于获取我们要查找的属性,因此我们可以使用索引访问类型,将DefinedFuncs
作为 object 类型,并将string
作为键类型:
type DesiredProp = DefinedFuncs[string];
/* type DesiredProp = {
name?: string | undefined;
events?: {
__schemaWorkaround__: null;
} | {
schedule: string | {
rate: string[];
enabled?: boolean;
name?: string;
description?: string;
input?: string;
};
} | undefined;
} */
Looks good: And of course we can do this all as a one-liner:看起来不错:当然我们可以一次性完成所有这些:
type DesiredProp = NonNullable<AWS["functions"]>[string];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.