Import type

by

in

Wild TypeScript caveat appeared!

In a destructuring type import, it apparently makes a difference whether the type keyword is inside or outside the curly braces:

import type { A } from "a" – import skipped

import { type A } from "a" – the import remains

It seems this is designed for cases where the import can have desired side effects, so you want to keep the import even if the imported type is lost in transpilation.

An examples from the TypeScript 5.0 release notes:

// Erased away entirely.import type { A } from "a";
// Rewritten to 'import { b } from "bcd";'import { b, type c, type d } from "bcd";

// Rewritten to 'import {} from "xyz";'
import { type xyz } from "xyz";

For me, this caused some confusion because I wanted to import a heavy module dynamically and also use types from it. I had import { parseConfig, type ConfigOptions } from "@/api/corpusConfig", then moved out the parseConfig to a dynamic import, but still did not get the module properly chunked by Vite. Moving the type keyword outside the curly braces did help.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *