Skip to content

Import and export TS types and interfaces in Next.js app don't needed anymore? #50684

Answered by icyJoseph
AngelinaPlax asked this question in Help
Discussion options

You must be logged in to vote

This is an obscure TypeScript feature. If a file has no import/export, then it is not a module, so it becomes global.

For example:

// import type {GetServerSideProps} from 'next'

interface Foo {
  bar(): void;
}

type Baz = {
  fizz(): void;
};

As long as the top line is commented, Foo and Baz will be visible from other files. The moment you uncomment that, this becomes a module and both Foo, and Baz, become private instances of the module. Likewise, the moment this file does an export, it turns into a module too.

This sort of reliance on global, or magically available, types, is anti-pattern that'll bring lots of chaos to your codebase. Avoid it.

--isolatedModules

https://www.typescrip…

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
1 reply
@AngelinaPlax
Comment options

Comment options

You must be logged in to vote
4 replies
@icyJoseph
Comment options

@icyJoseph
Comment options

@icyJoseph
Comment options

@AngelinaPlax
Comment options

Answer selected by AngelinaPlax
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
3 participants