Skip to content

Regression in 6.0: type inferred from const with broader type is too narrowΒ #63309

@trevorade

Description

@trevorade

πŸ”Ž Search Terms

"infer", "regression", "wide", "narrow"

πŸ•— Version & Regression Information

  • This changed between versions 5.9.3 and 6.0.2
  • This changed in commit or PR ???
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about YES
  • I was unable to test this on prior versions because N/A

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=6.0.2#code/C4TwDgpgBAggQlAvFA5DFUA+q4oNwBQBAxgPYB2AzsFACICiAYjAKoAyAKgFywLJr4ixADYBDSpSgBhKAG8CUOkjpNWnQgqgBbCMAAWpACYAKAJRzNiygHcAlsGJ7j+25QB0tc-MU+oxcdACPABGAE4QogDWhL6K-pSBuCHhUTE+AL6amelAA

πŸ’» Code

type AB = 'A' | 'B';

const DEFAULT: AB = 'A';

class C {
  D = DEFAULT;

  method() {
    switch(this.D) {
      case 'A': break;
      case 'B': break;
    }
  }
}

πŸ™ Actual behavior

C.D infers to "A"

πŸ™‚ Expected behavior

C.D infers to AB

Additional information about the issue

Somewhat common occurrence in Angular code.

Hundreds of instances of this class of error in Google.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions