Skip to content

String Literal issue with function overloads #8251

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

Closed
folini opened this issue Apr 22, 2016 · 4 comments
Closed

String Literal issue with function overloads #8251

folini opened this issue Apr 22, 2016 · 4 comments

Comments

@folini
Copy link

folini commented Apr 22, 2016

TypeScript Version:

TypeScript 1.8.30.0

Issue

The following code (copy & paste from the TypeScript documentation) doesn't work. It generates a compiler error.

function createElement(tagName: "img"): HTMLImageElement;
function createElement(tagName: "input"): HTMLInputElement;
function createElement(tagName: string): Element;
// ... more overloads ...

function createElement(tagName: string): Element {
    // ... code goes here ...
}

While, just moving the most general signature at the tip of the list will compile correctly.

function createElement(tagName: string): Element;
function createElement(tagName: "img"): HTMLImageElement;
function createElement(tagName: "input"): HTMLInputElement;
// ... more overloads ...

function createElement(tagName: string): Element {
    // ... code goes here ...
}
@Arnavion
Copy link
Contributor

What you have in the OP has no error (well, except for a missing return in the function body).

The code in the handbook does not have the tagName: string overload, which does result in TS2382: Specialized overload signature is not assignable to any non-specialized signature.. Is that what you meant?

(Using npm 1.8.10 which I believe corresponds to VS 1.8.30.0)

@yuit
Copy link
Contributor

yuit commented Apr 22, 2016

The example doesn't produce an error as @Arnavion points out. We made a change to such behavior. The behavior is in the "typescript@next".

@folini
Copy link
Author

folini commented Apr 22, 2016

Thanks @Arnavion and @yuit for taking the time to answer my question.

Apparently I changed the code when I copy & pasted removing the issue.
The issue appears when there is no declaration of the non-specialized signature, and there is only the implementation. This is the original code listed in the documentation that generates an error:

Code generating the error

function createElement(tagName: "img"): HTMLImageElement;
function createElement(tagName: "input"): HTMLInputElement;
// ... more overloads ...

function createElement(tagName: string): Element {
    // ... code goes here ...
    return;
}

Code with no error

function createElement(tagName: "img"): HTMLImageElement;
function createElement(tagName: "input"): HTMLInputElement;
function createElement(tagName: string): Element;
// ... more overloads ...

function createElement(tagName: string): Element {
    // ... code goes here ...
    return;
}

@sandersn
Copy link
Member

This was fixed in #6075.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants