question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Declaration emit should not inline type definitions

See original GitHub issue

TypeScript Version: 3.8.3, 3.8.1, probably others

Search Terms:

declaration inlining, dts inlining, declaration inline, inline literal, declaration literal

Code

// parent.ts
import { num, obj } from "./child"
export const reExportNum = num;
export const reExportObj = obj;
// child.d.ts
export declare const num: number;
export declare const obj: { a: 1 };

tsc index.ts --declaration

Expected behavior:

Declaration emit for parent.ts should not inline types.

// parent.d.ts
import { num, obj } from "./child"
export declare const reExportNum: typeof num;
export declare const reExportObj: typeof obj;

Actual behavior:

Today, declaration emit for parent.ts inlines the types and eliminates the import of the child.d.ts type definition.

// parent.d.ts
export declare const reExportNum: number;
export declare const reExportObj: {
    a: 1;
};

This is a correctness issue, because consumers of parent.d.ts will not get the correct types if the types in child.d.ts change.

In practice, this is most likely to happen when parent and child are in separate packages, because they are published independently, i.e. an application uses parent-package which uses types from child-package. This is exacerbated by the current practice on npm of parent-package depending on an unpinned version, using package.json dependency syntax "child-package": "*".

This issue was co-authored with @robpalme

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:5
  • Comments:18 (14 by maintainers)

github_iconTop GitHub Comments

3reactions
cyberixaecommented, Aug 27, 2020

I’m here because inlining everything caused my .d.ts file to be over 6.5MB long. The type checks started failing and I am wondering if the compiler simply ignores the end of the file because of some limitation.

1reaction
robpalmecommented, Jan 7, 2021

@aleksey-ilin the main solution I have found to solve huge declarations is to identify the root type that is inlined and then, assuming it is a statically known object type, create an interface from it and then refer to that interface at all usage sites.

export interface WrappedProblemType extends ProblemType {}

I have been experimenting with changing declaration emit so that shenanigans like this are not necessary. It kinda works and I’ll share that soon.


Separately, union and intersection types also get inlined. interface will not save you in this case - there is no reliable userland workaround for these. Thankfully there is work in progress to reduce the inlining of these specific types in https://github.com/microsoft/TypeScript/pull/42149

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use Typescript declaration types inline - Stack Overflow
Use it from the actual package that is being typed, not the @types/* module. For example, with react-router , you add @types/react-router ...
Read more >
TSConfig Reference - Docs on every TSConfig option
# Always Strict - alwaysStrict. Ensures that your files are parsed in the ECMAScript strict mode, and emit “use strict” for each source...
Read more >
Common Function Attributes (Using the GNU Compiler ...
The alias attribute causes the declaration to be emitted as an alias for another symbol, which must have been previously declared with the...
Read more >
Inline Functions, C++ FAQ
Why should I use inline functions instead of plain old #define macros? ... the declaration within the class body, next to the definition...
Read more >
Inline functions | Kotlin Documentation
Normal functions (not marked as inline) cannot have reified parameters. A type that does not have a run-time representation (for example, a non-reified...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found