Skip to content

[regression - 5.4] Generated .d.ts file contains invalid types #57843

Description

🔎 Search Terms

error "generic type" "requires" "type argument(s)" declaration .d.ts emit

🕗 Version & Regression Information

  • This changed between versions 5.3 and 5.4

⏯ Playground Link

bug workbench link

💻 Code

// @showEmit// @declaration: true// @showEmittedFile: repro.d.ts// @filename: base.tsexportabstractclassOne<D=unknown,S=unknown,M=unknown,DP=unknown,SP=unknown,>{}exportabstractclassTwo<D=any,S=any,M=any,DP=any,SP=any,>extendsOne<D,S,M,DP,SP>{readonlya!: D;readonlyb!: S;readonlyc!: M;abstractreadonlyd: Six<D,S>;}exportclassThree<D>{}exportclassFour<D>{}exporttypeFive<D,S>=Three<D>|Four<S>|undefined;exportabstractclassSix<D=unknown,S=unknown>{readonlye!: D;readonlyf!: S;}exporttypeSeven<S>=S;exportclassEight<D,S>extendsSix<Five<D,S>,Seven<S>>{}exporttypeNine<CextendsSix>=Eight<Ten<C>,Eleven<C>>;exporttypeTen<CextendsSix>=C["e"];exporttypeEleven<CextendsSix>=C["f"];exporttypeTwelve<T>=Nine<Fourteen<T>>;exporttypeThirteen<T>=Ten<Twelve<T>>;exportclassFourteen<T>extendsSix<void,T>{}exportinterfaceFifteen<T>{g: T;}exportclassSixteen<T=any>extendsTwo<Thirteen<T>,T,Fifteen<T>,never,never>{overridereadonlyd!: Eight<void,T>;}exporttypeSeventeen<TextendsTwentyOne>=T["h"];exporttypeEighteen={[k: string]: TwentyOne};exporttypeNineteen={[k: string]: Two};exporttypeTwenty=Record<string,Sixteen>;exportclassTwentyOne<T=any>{readonlyh!: T;}exportclassTwentyTwo<TextendsTwentyOne>extendsTwentyOne<Seventeen<T>|undefined>{}exportclassTwentyThree<D,S>extendsSix<D|undefined,S|undefined>{}exportclassTwentyFour<D,S,M,DP,SP>extendsTwo<D|undefined,S|undefined,M|undefined,DP|undefined,SP|undefined>{overridereadonlyd!: TwentyThree<D,S>;}exporttypeTwentyFive<TextendsTwo>=T["d"];typeTwentySix<VextendsEighteen,RextendsNineteen,AextendsTwenty>={[KinkeyofV]: Fourteen<Seventeen<V[K]>>;}&{[KinkeyofR]: TwentyFive<R[K]>}&{[KinkeyofA]: TwentyFive<A[K]>;};exporttypeTwentySeven<VextendsEighteen,RextendsNineteen,AextendsTwenty,>=ThirtyThree<TwentySix<V,R,A>>;exporttypeTwentyEight<VextendsEighteen,RextendsNineteen,AextendsTwenty,>=Eleven<TwentySeven<V,R,A>>;exporttypeTwentyNine<VextendsEighteen,RextendsNineteen,AextendsTwenty,>=Ten<TwentySeven<V,R,A>>;typeThirty<CextendsEighteen>={[kinkeyofC]: Fourteen<Seventeen<C[k]>>;};typeThirtyOne<CextendsTwenty>={[kinkeyofC]: TwentyFive<C[k]>;};exporttypeThirtyTwo<CextendsNineteen>={[kinkeyofC]: TwentyFive<C[k]>;};exportclassThirtyThree<SpecextendsThirtyFour>extendsSix<ThirtySix<Spec>,ThirtyFive<Spec>>{}exporttypeThirtyFour={readonly[k: string]: Six};exporttypeThirtyFive<DomainsextendsThirtyFour>={readonly[KinkeyofDomains]: Eleven<Domains[K]>;};exporttypeThirtySix<DomainsextendsThirtyFour>={readonly[KinkeyofDomains]?: Ten<Domains[K]>;};exporttypeThirtySeven<TextendsTwo>=T["c"];exporttypeThirtyEight<TextendsTwo>=T["b"];exporttypeThirtyNine<VextendsEighteen,RextendsNineteen,AextendsTwenty,>={readonly[KinkeyofV]: Seventeen<V[K]>}&{readonly[KinkeyofR]: ThirtySeven<R[K]>;}&{[KinkeyofA]: ThirtyEight<A[K]>};exportclassForty<VextendsEighteen,RextendsNineteen,AextendsTwenty,>extendsTwo<TwentyNine<V,R,A>,TwentyEight<V,R,A>,ThirtyNine<V,R,A>,never,never>{overridereadonlyd!: ThirtyThree<Thirty<V>&ThirtyTwo<R>&ThirtyOne<A>>;}exportdeclarefunctiondoThing<D,S,M,DP,SP>(arg: Two<D,S,M,DP,SP>,): TwentyFour<D,S,M,DP,SP>;// @filename: repro.tsimport{doThing,typeForty}from"./base";exportconstbar=doThing({}asForty<{},{},{}>);// @filename: repro_output.d.tsexportdeclareconstbar: import("./base").TwentyFour<import("./base").TwentyNine<{}&import("./base").ThirtyTwo<{}>&{}>,import("./base").TwentyEight<{}&import("./base").ThirtyTwo<{}>&{}>,import("./base").ThirtyNine<{},{},{}>,unknown,unknown>;

🙁 Actual behavior

The code emitted for repro.d.ts is

exportdeclareconstbar: import("./base").TwentyFour<import("./base").TwentyNine<import("./base").Thirty<{}>&import("./base").ThirtyTwo<{}>&import("./base").ThirtyOne<{}>>,import("./base").TwentyEight<import("./base").Thirty<{}>&import("./base").ThirtyTwo<{}>&import("./base").ThirtyOne<{}>>,import("./base").ThirtyNine<{},{},{}>,unknown,unknown>;

This code has two type errors (on lines 2-6 and 7-11 respectively):

Generic type 'TwentyNine' requires 3 type argument(s).
Generic type 'TwentyEight' requires 3 type argument(s).

Note how the generated type uses TwentyNine on L2 and TwentyEight on L7.
These are the incorrect types to use (see below)

🙂 Expected behavior

The types generate valid code with no semantic errors.
For example intellisense reports this type for bar which is valid

exportdeclareconstbar: import("./base").TwentyFour<import("./base").ThirtySix<import("./base").Thirty<{}>&import("./base").ThirtyTwo<{}>&import("./base").ThirtyOne<{}>>,import("./base").ThirtyFive<import("./base").Thirty<{}>&import("./base").ThirtyTwo<{}>&import("./base").ThirtyOne<{}>>,import("./base").ThirtyNine<{},{},{}>,unknown,unknown>;

Note how this type instead uses ThirtySix on L2 and ThirtyFive on L7.

Additional information about the issue

Sorry that this example is so goddamn disgusting to look at.
This is the name-mangled version of some real code from our codebase.
I tried to minimise it but it's all such an intermingled spaghetti mess that I wasn't able to figure out what things I could delete without changing the output.

For context - I am working on upgrading our codebase to TS5.4.
When I ran our typecheck CLI I got a number of errors across the codebase.
When I opened the files with errors - those errors didn't show up in the IDE.
I spent a while pulling my hair out trying to figure out why there was a discrepancy.

The error messages led me back to a monster file which makes use of some really ugly patterns of inferred types from typeofs to generate a lot of types. For reference the file itself is >3k LOC and the .d.ts file it generates is >11k LOC. So so so many anonymous, inferred types.

When I opened the .d.ts file it was filled with type errors. From what I can tell what's happening is that when you open the files with errors in the IDE then TS uses the .ts source files for type checking and so it uses the "correct" type for everything. When we run our CLI to do the typecheck it uses project references and so it uses the .d.ts output instead.

Because the .d.ts output doesn't match the types TS infers from the .ts file this causes the discrepancy between the reported errors.

I think a most (if not all) of the errors in the .d.ts file are variations of the error shown by this repro but it's so hard to know as there are hundreds of them.

Note: I didn't write this code and I hate that it exists. I am sorry.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Fix AvailableA PR has been opened for this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions