Skip to content

Type check fails when union type has more than 12 literals #61031

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
hrjakobsen opened this issue Jan 23, 2025 · 2 comments
Closed

Type check fails when union type has more than 12 literals #61031

hrjakobsen opened this issue Jan 23, 2025 · 2 comments

Comments

@hrjakobsen
Copy link

hrjakobsen commented Jan 23, 2025

πŸ”Ž Search Terms

"12 limit union type", "union type subtyping literals"

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about union types

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.8.0-dev.20250123#code/C4TwDgpgBAYglgGwRAJgSQMYHsB2UC8UA3gFBTlRzY4BcUAzsAE5w4DmZFArjgGaLIUAfjq8AhgnrQSAXxIlQkKAFU+A1JlwAVcNEKdyAHygByKrgD6ARhNQDUY2eoWATCfuPzOCwGZ3FB1MvCwAWfwpPZwBWcKMg5wA2WMCnSwB2ZMjLAA5M+MsATjzU7ysABmLgqxsPfNK3WpLrP3lFaFV+JA1qAmJ7LzoO9XRqHUh7Hk7BOmYuCFlW3ShNPEJ4LpHcQKGNlfleHgxgOC2MJggxYAgVgAoBlTVd0d0AGihJ4boAIywsZDEcABKKB0FZ9ALnYBcJh4UgBAJeF72AIfDb2ORyIA

πŸ’» Code

type FilledIcon = {
    icon: string
    unfilled?: false 
}

type UnfilledIconType = 
    | 'icon_1' 
    | 'icon_2'
    | 'icon_3'
    | 'icon_4'
    | 'icon_5'
    | 'icon_6'
    | 'icon_7'
    | 'icon_8'
    | 'icon_9'
    | 'icon_10'
    | 'icon_11'
    | 'icon_12'
    | 'icon_13'

type UnfilledIcon = {
    icon: UnfilledIconType
    unfilled: true
}

type Icon = FilledIcon | UnfilledIcon

function createIcon(icon: UnfilledIconType, unfilled: boolean) : Icon {
    return {
        icon,
        unfilled
    }
}

πŸ™ Actual behavior

Typescript reports the following error:

Type '{ icon: UnfilledIconType; unfilled: boolean; }' is not assignable to type 'Icon'.
  Type '{ icon: UnfilledIconType; unfilled: boolean; }' is not assignable to type 'UnfilledIcon'.
    Types of property 'unfilled' are incompatible.
      Type 'boolean' is not assignable to type 'true'.(2322)

πŸ™‚ Expected behavior

Should type check without problems.

{ icon: 'icon_1' unfilled: true } would be a valid UnfilledIcon, and { icon: 'icon_1' unfilled: false } would be a valid FilledIcon.

Additional information about the issue

This only seems to happen if UnfilledIconType has more than 12 literals. Removing icon_13 causes the type check to succeed.

@jcalz
Copy link
Contributor

jcalz commented Jan 23, 2025

This is behaving as designed. Duplicate of #43283 and others. The limit is 25 and you are using your union twice, so 13x2 breaks it.

@hrjakobsen
Copy link
Author

Thanks for the clarification and quick response @jcalz!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants