Skip to content

A strange behavior when checking extends ReadonlyArray<any>Β #59430

Closed as not planned

Description

@ruojianll

πŸ”Ž Search Terms

"extends", "ReadonlyArray"

πŸ•— Version & Regression Information

  • This changed between versions 5.5.3

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAglC8UBKECGATA9gOwDYhgCdDUQAeAchgoD4AoUSKAIQWTSzwONMudobhoMAMYiIYYDDIAVKBAAewCNnQBnWDTYzBTGRDXAAjG1HjJ0mFoD011GInL0u6PsMAmUw4tlmN6xDEmIQuUG7AAMxe5lK+8koq6uwYOPhEJOSo2CBaAPwsUABcLP5Q9jEQ6EA

πŸ’» Code

type A = ReadonlyArray<'A'>
type B = ReadonlyArray<'B'>
type AcceptA<T extends A> = T
type Test1 = AcceptA<A> //accepted
type Test2 = AcceptA<B> //error
type Test3 = AcceptA<B extends ReadonlyArray<any> ? B : B> // accepted

πŸ™ Actual behavior

It could be accepted in Test3 but it's still the same B in Test2 which has an error.

πŸ™‚ Expected behavior

Show an error as Test2.

Additional information about the issue

No response

Activity

  1. changed the title [-]A strange behavior when checking `extends Readonly<any>`[/-] [+]A strange behavior when checking `extends ReadonlyArray<any>`[/+] on Jul 26, 2024
  2. jcalz commented on Jul 26, 2024

    @jcalz
    Contributor

    SO question

    Ryan Cavanaugh (@RyanCavanaugh) Are we sure this is a bug? The type B extends ReadonlyArray<any> ? B : B is a substitution type like B & ReadonlyArray<any> (due to #37348), and that intersection is indeed allowed in AcceptA because of the unsoundness of any. It's weird, but why would someone write B extends ReadonlyArray<any> ? B : B in the first place except to expose some substitution-type behavior?

  3. added
    Not a DefectThis behavior is one of several equally-correct options
    and removed
    BugA bug in TypeScript
    on Jul 26, 2024
  4. RyanCavanaugh commented on Jul 26, 2024

    @RyanCavanaugh
    Member

    Joe Calzaretta (@jcalz) yeah, good observation. I agree

  5. ruojianll commented on Jul 27, 2024

    @ruojianll
    Author

    Joe Calzaretta (@jcalz) Ryan Cavanaugh (@RyanCavanaugh) But isn't it a bug? Test3 shouldn't accept B in result.

  6. ruojianll commented on Jul 27, 2024

    @ruojianll
    Author

    The true value B is should not be B & ReadonlyArray<any>. If the condition is true, why do we need to narrow down the true value?

  7. ruojianll commented on Jul 29, 2024

    @ruojianll
    Author

    I still consider it as a bug. Anders Hejlsberg (@ahejlsberg)

  8. typescript-bot commented on Aug 1, 2024

    @typescript-bot
    Contributor

    This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  9. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions