Skip to content

Can't Narrow Destructured Tuple Union #56973

Description

🔎 Search Terms

Narrowing, Union, Destructuring, Tuple

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about unions, instanceof and tuples.

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.0.4#code/MYewdgzgLgBAagQQDIFUCiBlGBeGBtAIgEMBGAgGhmICYKqiBmOggIzMtdo5aY+HarAugpgF0YRCDFCQoAbgBQCgPTKYABQA2AU0naYASwDmYEACd9UABb7TYALQRtkA8CKaYYIlACuFmCAAZjDW2k4hAJ4ADmEAdDAAKlYGUikSMBAGALZRmgaBBtoAJjAAbtpmmeABwVkRMBbuMD7hbk6xClDR+nDuPvq4ABRdMUHwyOgYAJR4YD5ZLBWiiiP6CWZEkIHmWQBiPmDAUAbVQ6V9YQBc8Bd4olM4AHw3mv13K92JG1s7ACLaBTABmOp3wACVtEY0AAPKLiAA++HWmwg2zMewORxOYGWSlUiWSUiKIDCYAA5LAAO7mADWlBYPlgTm0WSkeRp+mBGSsIB8miKHUCmJBYBgFTM5hhRByOkGRQBBiBIuuyJ+6P+gOB2KmKu+qJ2+0OIpgAG8FDAYDpYHgZEUteBxLh5ZqRYpzYZgoNbfbRYroJtgNoxhCobCHmaLRaLL4zKLBg9sM9EKhMLECpooBVBoNzq9tAnnt6RbFM9AcxcplNFBaAL5KKPaGOiovYxR1lRqJJpalmGkQSibEppdxZEDQEI2DLSyzJMBGBRCo3YjI+YCBiAQKUy7SDG3gO0i0S6lForIaxU+nVfE8G4XLiMemBe-c+wyyANB4IhmFRcPuhtNk+BbjCmGBpgYGZZuWebAS24AlmEUDQf0lbVjA7YAX4zYvq6Cg1kAA

💻 Code

constVALUES=["a1","a2","a3","b1","b2","b3","c1","c2","c3"]asconst;// Please ignore the non-sensical nature of these types. This is a simplified version of my real use case.typeValue=(typeofVALUES)[number];typeTransformFunction=(values: Value[])=>Value[];typeTransformDefinition=[RegExp]|[TransformFunction];// This doesn't work, but seems like it should.functionerrorExample(definition: TransformDefinition): TransformFunction{let[condition]=definition;if(conditioninstanceofRegExp){return()=>VALUES.filter((value)=>condition.test(value));}returncondition;}// This works, and is almost the same thingfunctionsuccessExample([condition]: TransformDefinition): TransformFunction{if(conditioninstanceofRegExp){return()=>VALUES.filter((value)=>condition.test(value));}returncondition;}

🙁 Actual behavior

In the first example function, I get this error:

Property 'test' does not exist on type 'RegExp | TransformFunction'.
Property 'test' does not exist on type 'TransformFunction'.

🙂 Expected behavior

Both examples should compile without an error

Additional information about the issue

My real use case is a bit more complex, and involves tuples with multiple values.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions