Skip to content

Intrinsic string mapping type with type parameters is incorrectly resolved in conditional types #55847

@sankantsu

Description

@sankantsu

Bug Report

🔎 Search Terms

lowercase, uppercase, intrinsic, conditional type

🕗 Version & Regression Information

  • This changed between versions 5.0.4 and 5.1.0-beta.
  • 5.2.2 and nightly still have the same problem.
  • I also reviewed the FAQ for entries about lowercase, uppercase, intrinsic and conditional types.

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.1.6#code/C4TwDgpgBAggPAZQHxQLxQDIHsDuEBOAxgIYDOEiAZKcPgJYB2A5ihAB7AQMAmpUARADMsWflAD8UAIxQAXFAAMAKAD0KqFAB64paEhQAQohTpseImQoJqtRiyjtOPPgANBAEgDeNeswC+LhLScoqq6lo6SgA2EMBQbFLy8EIi-CbBag74+Fj40bHxAEzyRimi6TKZBDn4QA

💻 Code

type A<S> = Lowercase<S&string> extends "foo" ? 1 : 0
//  ^?  type A<S> = 0
type B<S> = Lowercase<S&string> extends `f${string}` ? 1 : 0
//  ^? type B<S> = 0

let x1: A<"foo"> = 1 // error
let x2: B<"foo"> = 1 // error

🙁 Actual behavior

A<"foo"> = 0 and B<"foo"> = 0.
Actually, these conditional branches are resolved at the time of type alias definition.
Then, any instantiation of A and B is 0

🙂 Expected behavior

Both A<"foo"> and B<"foo"> should be 1.
Conditional types in A and B should not be resolved during type alias definition, but should be deferred until an instantiation.

Additional information about the issue

Related

#52102 is similar.
This issue is about incorrect "simplification" involving intrinsic string mapping types.

More info

I did some investigation in codebase.
It looks like that the cause of the problem is handling of string mapping type in getGenericObjectFlags or isPatternLiteralPlaceholderType which was introduced in #52836 .

https://github.dev/microsoft/TypeScript/blob/97147915ab667a52e31ac743843786cbc9049559/src/compiler/checker.ts#L17967

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions