microsoft / microsoft/TypeScript

String literal type inconsistency in overload resolution

Open
#9,443 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

As far as I see, the TS compiler still sees a string literal type of a single literal as an old style "string parameter" thing. I thought that the concept of string literal types should replace overloading on string parameters, but seems that it doesn't.

That complicates things, and results in a "strange" behaviour in overload resolution:

class A256 {
    m(a): any;
    m(a: "a" | "b"): string;
    m(a: "c"): string;
    m(a): any {}
}

var foo1 = new A256().m("a"); // 'any'
var foo2 = new A256().m("c"); // 'string'

What should be the correct logic with string literal types in overload resolution?
Shouldn't overloads with unions of literals precede the others as well, when resolving overloads?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling the A256 example and compare the inferred types of foo1 and foo2 with the expected overload behavior. Then trace the compiler's overload-resolution handling for string literal unions; done means the behavior is defined and the example resolves consistently with that rule, with regression coverage added if the relevant test location is identified.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.