microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.29k stars 12.39k forks source link

Creating too many types for DOM & D3 #34586

Open amcasey opened 4 years ago

amcasey commented 4 years ago

Commit 06fe1ed53721862b56c61ebb89b1daee2bc10710 introduced a regression in the number of types created when compiling a snippet of @types\d3. Specifically, it appears to be elaborating types for an error message that ends up getting dropped.

Repro:

{
  "compileOnSave": false,
  "compilerOptions": {
    "alwaysStrict": true,
    "noImplicitAny": true,
    "noEmit": true,
    "lib": [ "es5", "dom" ],
    "noResolve": true,
    "types": [],
    "skipLibCheck": true
  },
  "files": [
    "test.ts",
}

test.ts

declare function select<GElement extends BaseType, OldDatum>(selector: string): Selection2<GElement, OldDatum, HTMLElement, any>;

type BaseType = Element | Document | Window | null | number;

type ValueFn<T extends BaseType, Datum, Result> = (this: T, datum: Datum, index: number, groups: T[] | ArrayLike<T>) => Result;

interface Selection2<GElement extends BaseType, Datum, PElement extends BaseType, PDatum> {
    select<DescElement extends BaseType>(selector: ValueFn<GElement, Datum, DescElement>): Selection2<DescElement, Datum, PElement, PDatum>;
}

For an apples-to-apples comparison, use nolib and specify the 3.1 lib.es5.d.ts and lib.dom.d.ts for both compilations.

In round numbers, there are ~150 types without the change and ~2500 types with the change.

amcasey commented 4 years ago

FYI @DanielRosenwasser

amcasey commented 4 years ago

Note that 06fe1ed53721862b56c61ebb89b1daee2bc10710 can be rolled back without losing (all of) the functionality it provides because it was a revision of another commit in the same PR.