Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Difference in overload pick between JS and TS with default generic type parameter #39509

Open
IllusionMH opened this issue Jul 8, 2020 · 1 comment
Assignees
Labels
Needs Investigation This issue needs a team member to investigate its status.

Comments

@IllusionMH
Copy link
Contributor

TypeScript Version: 4.0.0-dev.20200703, 3.9.2 (playground)

Search Terms: js ts overload difference, javascript different overload, javascript wrong overload, javascript overload generic empty object

Code
definition.d.ts

export class EmptyObjectOverloads<T = {}> {
    constructor(a: Readonly<T>);
    /** @deprecated */
    constructor(a: T, b?: any);
}

export class RecordOverloads<T = Record<string, any>> {
    constructor(a: Readonly<T>);
    /** @deprecated */
    constructor(a: T, b?: any);
}

ts-example.ts

import { EmptyObjectOverloads, RecordOverloads } from './declaration';

class TSExampleEmpty extends EmptyObjectOverloads {
    constructor(a: any) {
        // hover over super: constructor EmptyObjectOverloads<{}>(a: Readonly<{}>): EmptyObjectOverloads<{}> (+1 overload)
        super(a); 
    }
}

class TSExampleRecord extends RecordOverloads {
    constructor(a: any) {
        // hover over super: constructor RecordOverloads<Record<string, any>>(a: Readonly<Record<string, any>>): RecordOverloads<Record<string, any>> (+1 overload)
        super(a);
    }
}

js-example.js

class JSExampleEmpty extends EmptyObjectOverloads {
    /** @param {*} a */
    constructor(a) {
        // hover over super: constructor EmptyObjectOverloads<any>(a: any, b?: any): EmptyObjectOverloads<any> (+1 overload)
        super(a); // deprecation warning '(a: any, b?: any): EmptyObjectOverloads<any>' is deprecated ts(6385)
    }
}

class JSExampleRecord extends RecordOverloads {
    /** @param {*} a */
    constructor(a) {
        // hover over super: constructor RecordOverloads<Record<string, any>>(a: Readonly<Record<string, any>>): RecordOverloads<Record<string, any>> (+1 overload)
        super(a);
    }
}

Expected behavior:
Both TSExampleEmpty and JSExampleEmpty pick same overload similar to TSExampleRecord and JSExampleRecord.

Actual behavior:
TSExampleEmpty uses default generic value {} and picks first overload.
For JSExampleEmpty generic value to is any and second (deprecated) overload picked, while JSExampleRecord works as expected.

In real world examples this behavior marks all super(props); in React class components in JS files as deprecated.
Of course it's impossible to report TS issue without mentioning React 😆 related constructor declaration

Playground Link:
Example TS Example JS

Related Issues: Haven't found any

@DanielRosenwasser DanielRosenwasser added the Needs Investigation This issue needs a team member to investigate its status. label Jul 8, 2020
@DanielRosenwasser
Copy link
Member

I vaguely recall generic defaults being treated differently in JS mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

No branches or pull requests

3 participants