Dynamically access types from an `import * as` in Typescript?

Viewed 88

Say I have a models.ts like so:

class Foo {
    name?: string;
}

type FooDocument = { name: string };

export { Foo, FooDocument };

Where each "Document" of a class type has the serialized interface (no Functions/getters, etc)

Now when I import the models in a different file, I seem to be unable to access those types by name?

import * as models from './models'

// Works!
type A = models.FooDocument  

// No worky
type B = models['FooDocument'] // Cannot use 'models' as a type ts(2709)

// Still no worky :(
type C = typeof models['FooDocument'] // Property 'FooDocument' does not exist on type 'typeof import('./models')' ts(2339)

Is this just a limitation of Typescript or am I doing something wrong?

For convenience, I made a CodeSandbox with this setup:

https://codesandbox.io/s/blissful-dijkstra-3u0fq?file=/src/index.ts

0 Answers
Related