Skip to content

Intersection types and string literals #9410

Closed
@Quantumplation

Description

@Quantumplation

TypeScript Version: 1.8.0

Code

// A *self-contained* demonstration of the problem follows...
class MyClass<T> {
  value: T & string
}

var myInstance: MyClass<"A" | "B">
myInstance.value = "A" // Case 1
myInstance.value = "B" // Case 2
myInstance.value = "other value" // Case 3

Expected behavior:
(prefix: I'm not sure if this was intentional or not, or whether it's a known issue or not, and I looked through other issues looking for mention of it and didn't see anything)

Case 1 and 2 should be valid, while case 3 should be an error.

Actual behavior:
All three cases are errors, as the intersection results in an empty type, rather than the type "A" | "B"
From one perspective, the type "string" is a super set of type "A" | "B".

This might also impact the recent work done in #9407

If this was intentional behavior, i'll edit this issue into a question as to why that decision was made, so it can serve as future documentation for anyone else searching for this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issueSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions