Isolated Declarations

TypeScript Logo

--isolatedDeclarations for standalone DTS emit:

`--isolatedDeclarations` for standalone DTS emit · Issue #47947 · microsoft/TypeScript
Suggestion TypeScript supports relying on type inference to produce (parts of) the API of a module. E.g. users can write (sorry, slightly contrived): // in counter.ts: import {Splitter} from 'textu...
`--isolatedDeclarations` for standalone DTS emit · Issue #47947 · microsoft/TypeScript favicon https://github.com/microsoft/TypeScript/issues/47947
`--isolatedDeclarations` for standalone DTS emit · Issue #47947 · microsoft/TypeScript
github.com
github.com favicon https://github.com/microsoft/TypeScript/pull/53463

since TypeScript 5.5:

Announcing TypeScript 5.5 - TypeScript
Today we’re excited to announce the release of TypeScript 5.5! If you’re not familiar with TypeScript, it’s a language that builds on top of JavaScript by making it possible to declare and describe types. Writing types in our code allows us to explain intent and have other tools check our code to catch mistakes like […]
Announcing TypeScript 5.5 - TypeScript favicon https://devblogs.microsoft.com/typescript/announcing-typescript-5-5/#isolated-declarations
Announcing TypeScript 5.5 - TypeScript

Overview

What is isolated declarations?

Today, TypeScript has a many features to help you write better code.
Among them, "type inference" is a particularly important feature.

However, type inference has some problems.

  • incurs significant costs
  • locked into tsc

At first, costs.
It goes without saying, type inference comes with a cost.
And it is prominent in large code bases.
Tsc must analyze all the code in your project to infer types.

Second, locked into tsc.
There is no alternative to tsc.
There is a famous story about stc wa closed.

TypeScript type checker · Issue #571 · swc-project/swc
Closing as the stc is now abandoned. TypeScript was not something that I could follow up on in an alternative language.
TypeScript type checker · Issue #571 · swc-project/swc favicon https://github.com/swc-project/swc/issues/571#issuecomment-1915966297
TypeScript type checker · Issue #571 · swc-project/swc

TypeScript was not something that I could follow up on in an alternative language.

There are several alternative projects in development, but once completed, they are expected to significantly improve performance.
However, due to various issues such as the lack of a clear specification for TS and the strong resources of Microsoft (implementation not catching up), there is currently no practical alternative to tsc.

New Approach

The new approach is to use "Isolated Declarations".

Isolated declarations (via --isolatedDeclarations option) enforce to write explicit type annotations.
This alleviates the cost of type inference.

There are similar approaches in deno called 'slow types'.

About "slow types" - Docs - JSR
JSR uses TypeScript types to generate documentation and improve Node.js compatibility. "Slow types" can get in the way of this.
About "slow types" - Docs - JSR favicon https://jsr.io/docs/about-slow-types

When we emits type declarations (.d.ts), in the past we use --declaration option.
But there are several issues mentioned above.

Just to summarize more specific details below:

  • performance
    • cross-file type inference (export/import)
    • parallel type inference

For this problem, Isolated Declarations enforces explicit type annotations.

@typescript-eslint/parser | typescript-eslint
An ESLint parser used to parse TypeScript code into ESLint-compatible nodes, as well as provide backing TypeScript programs. ✨
@typescript-eslint/parser | typescript-eslint favicon https://typescript-eslint.io/packages/parser/#experimental_useprojectservice
@typescript-eslint/parser | typescript-eslint
© 2024-PRESENT ubugeeei. All rights reserved.