简体   繁体   English

如何在 Typescript 中的现有类型中选择或访问索引器/索引签名属性

[英]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 .因为functionsAWS可选属性,所以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中过滤掉nullundefined

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];

Playground link to code 游乐场代码链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM