如何将类型定义为像[key:string]键/值结构-TypeScript这样定义的对象键列表

如何将类型定义为像[key:string]键/值结构-TypeScript这样定义的对象键列表,typescript,typescript-typings,typescript2.0,typescript-generics,Typescript,Typescript Typings,Typescript2.0,Typescript Generics,我想像这样在TypeScript中定义接口,但我不知道怎么做: export interface SomeInterface { testProp:{ [key: string]: { prop1: string; prop2?: string; prop3?: string; .... }; } requiredProps: ???? // <- here i would like to define type to be array o

我想像这样在TypeScript中定义接口,但我不知道怎么做:

export interface SomeInterface {
  testProp:{
  [key: string]: {
    prop1: string;
    prop2?: string;
    prop3?: string;
    ....

  };
}
  requiredProps: ???? // <- here i would like to define type to be array of used keys
}

我试图将requiredProps定义为
requiredProps:[keyof Pick没有与您尝试执行的操作相对应的特定类型。但是,您可以将其表示为一个类型。例如:

export interface SomeInterface<K extends PropertyKey, R extends K> {
    testProp: Record<K, {
        prop1: string;
        prop2?: string;
        prop3?: string;
    }>;
    requiredProps: R[]
}
然后像这样使用它:

const value = asSomeInterface({
    testProp: {
        orange: {
            prop1: 'test1',
        },
        kiwi: {
            prop1: 'random text',
        },
        lemon: {
            prop1: 'text',
        },
    },
    requiredProps: ["orange", "kiwi"]
});
当您向
requiredProps
添加不是
testProp
键的元素时,可以看到所需的错误:

asSomeInterface({
    testProp: {
        a: { prop1: "" }, b: { prop1: "" }, c: { prop1: "" }
    },
    requiredProps: ["a", "b", "c", "d"] // error!
    // --------------------------> ~~~~
    // Type '"d"' is not assignable to type '"a" | "b" | "c"'
})

制作<代码>某个接口< /> >一个泛型类型更复杂,因为处理它们的任何值或函数都需要携带额外的类型参数。您可能只考虑使用对其输入不能保证正确的外部用户的通用代码,然后在内部将类型扩展为非泛型版本WHIC。h不太安全,但更容易通行:

// code seen by outside users, enforces constraint
function externalFunction<K extends string, R extends K>(
  someInterface: SomeInterface<K, R>
) {
    internalFunction(someInterface)
}

// code not exposed outside, widens to non-generic version
type SomeWiderInterface = SomeInterface<string, string>
const someWiderValue: SomeWiderInterface = value; // accepted
function internalFunction(someWiderInterface: SomeWiderInterface) {
    // do stuff
}
//外部用户看到的代码强制执行约束
函数外部函数(
someInterface:someInterface
) {
内部函数(someInterface)
}
//代码未暴露在外部,扩展到非泛型版本
键入SomeWiderInterface=SomeInterface
const someWiderValue:SomeWiderInterface=value;//已接受
函数内部函数(someWiderInterface:someWiderInterface){
//做事
}

asSomeInterface({
    testProp: {
        a: { prop1: "" }, b: { prop1: "" }, c: { prop1: "" }
    },
    requiredProps: ["a", "b", "c", "d"] // error!
    // --------------------------> ~~~~
    // Type '"d"' is not assignable to type '"a" | "b" | "c"'
})
// code seen by outside users, enforces constraint
function externalFunction<K extends string, R extends K>(
  someInterface: SomeInterface<K, R>
) {
    internalFunction(someInterface)
}

// code not exposed outside, widens to non-generic version
type SomeWiderInterface = SomeInterface<string, string>
const someWiderValue: SomeWiderInterface = value; // accepted
function internalFunction(someWiderInterface: SomeWiderInterface) {
    // do stuff
}