Coming from Java to TS, I've omitted the {...}
around the imported type.
import DiscriminatorMappingData from './DiscriminatorMappingData';
instead of
import {DiscriminatorMappingData} from './DiscriminatorMappingData';
See TypeScript - storing a class as a map value?.
I've read the documentation and didn't understand much. I only took from it that when I only need one type from a file, I can omit the {}
.
However, that caused weird errors, like "Unknown name", or unexpected type incompatibilites.
So, what's the difference, put simply?
This is imports in
destructruring
format. Essentially grouping all the entities that we want to be importedfrom
a definition file.If you are familiar with other programming languages you might be familiar with the destructuring notion. Actually
destructuring
was added in the form of assignments as part ofes6
.Import restructuring support was first suggested in TypeScript in this Github ticket but then got grouped under this ticket that was tracking all the
es6 modules
.The difference between your two
import
declarations is covered in the TypeScript specification. From §11.3.2, Import Declarations:Thus, you would omit the braces only when you are importing something that was exported as the
default
entity of the module (with anexport default
declaration, of which there can only be one per module). The name you provide in theimport
declaration becomes an alias for that imported entity.When importing anything else, even if it's just one entity, you need to provide the braces.
The Default exports section of the TypeScript handbook has a few examples.