3

Is there way to union all imported types from module? Like this

import * as types from "./types";

type Union_ = Union<types> // type1 | type2 | ...

I also know that only types and interfaces will be exported from this file.

Supremus
  • 131
  • 1
  • 8

2 Answers2

1

No it's not possible.
You have to do named imports.
One possible solution is to union all the types in your types.ts file and export it from there and use it in this file.

Mahdi Ghajary
  • 2,717
  • 15
  • 20
0

I had this same question: Is there a good way to generate types by importing all variables from a file in typescript

There are a couple of ways that you can do this, but ideally you'd avoid wildcard imports in anything that will be bundled into your build.

What I ended up not going with:

import * as types from './types';

const typeValues = Object.values(types);
export type FooConst = typeValues[number];

The other idea I was working on would involve compilation to generate a union type based on the imports. something like tsc

kvanost
  • 11
  • 2