ITADN

Exhaustiveness check fails when matching arrays with 2 or more elements

#322Openallan-cannon 创建于 2025-05-14
A
allan-cannoncommented
**Describe the bug** This code fails the exhaustiveness check even though all cases have been covered. In this case my intent is to use up to the first 2 strings in an array of unknown length with different handling depending on the number of strings. ```typescript const stringList: string[] = []; match(stringList) .with([], () => {}) .with([P.string], () => {}) .with([P.string, P.string], () => {}) .with([P.string, P.string, ...P.array(P.string)], () => {}) .exhaustive(); // Type 'NonExhaustiveError<[string, ...string[]]>' has no call signatures. ``` The actual matching appears to be working as expected, just the exhaustiveness check is wrong. For example this code resolves the exhaustiveness check but the last `with` branch is unreachable. ```typescript const stringList: string[] = []; match(stringList) .with([], () => {}) .with([P.string], () => {}) .with([P.string, P.string], () => {}) .with([P.string, P.string, ...P.array(P.string)], () => {}) .with([P.string, ...P.array(P.string)], () => {}) // This branch is unreachable .exhaustive(); // No exhaustiveness error ``` This code "works" but the type of the second item is `string | undefined` which is not what I'm looking for. ```typescript const stringList: string[] = []; match(stringList) .with([], () => {}) .with([P.string], () => {}) .with([P.string, ...P.array(P.string)], ([first, second]) => {}) // second is type string | undefined .exhaustive(); // no exhaustiveness error ``` **TypeScript playground with a minimal reproduction case** [Playground](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbziAhjAxgCwDRwApwC+cAZlBCHAOQwDOAtGGjAKZQB2VAUFyQK7t0MYBHZxWtGAAoANsEkAuOJKjB2AcwDaAXQCUiLnCNwoLGHw7I0WWfJi7DxgHQB3YDExSduKfoC8AHxw6KK0EDIsTjIQ6lJULOAwAJ5wcpJUug7GcK7unpp4Tipq6to+-kEh7GERUTFxafBuHnCiLHDuCRlZzs35hcUauAMwqhplcL5wgcGh4ZHRsVSNcH3iLhAdrCC03Y5GuR5eI2Pqw0WjJbhON4UoUFAoSb4TUzNVNQv1y3areeubaDIaDtTo7PbZJwsAAemBQfEkwAAbixfABuLiEHj8QTCUTiFiSADqeSJ0AA1vcIAIACa2RTKS4aHT6BD7ExmCxiVAYTyNHoHPpeV4VWbVeZ1JYJMDJVJ2CG9PLHC6nEXTSpzWqLBq-NZtLZdTLsw79FVXfBm8bldViz6SnWSP4tGAbA3go2QoUFS1nC2DX03Jx3B5PF7W96ar5LFZrF2AqDA0xu3YexVHb3+663Jz3R7PXRqiPirXfGP-fVAkAg5OTASmFBYFAAIwisxpLF0CoOMLhCOEKPRmJ4Hwl2okMBJHjJUEp5FpwsyGJHJdi48nmGns+p7DpmioAEYqHpdEvI-a16SKVT53vD7gqAAmI+LrjLqNSC9Tq9zndeA9Ue8n3vABmZ8T1fM8x0JCdLxna9f1vADqCA6hQPvAAWMC0SAA) **Versions** - TypeScript version: 5.8.3 - ts-pattern version: 5.7.0 - environment: chrome
0 条评论