Dependent types in .d.ts files

Viewed 34

Is it possible to use dependent types in .d.ts files?

I wish to describe the following relationship:

interface FooOptions {
    members: Record<string, string>;
}

class Foo {
    constructor(options: FooOptions);
    private options: FooOptions;
    public members: Record<keyof typeof options.members, string>
}

Such that accessing the arbitrarily named entries in the members map passed in the options interface are accessible through the field on the class. I.e.:

const test = new Foo({ members: { a: 23, b: 42 } });
test.members.a // => 23
test.members.c // => fails

The above does not compile, due to TS2552: Cannot find name 'options'.

0 Answers
Related