-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[...T[], T] does not extend [T, ...T[]] #60463
Copy link
Copy link
Open
Labels
Domain: check: Type InferenceRelated to type inference performed during signature resolution or `infer` type resolutionRelated to type inference performed during signature resolution or `infer` type resolutionHelp WantedYou can do thisYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone
Metadata
Metadata
Assignees
Labels
Domain: check: Type InferenceRelated to type inference performed during signature resolution or `infer` type resolutionRelated to type inference performed during signature resolution or `infer` type resolutionHelp WantedYou can do thisYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases
🔎 Search Terms
homogeneous tuple, non-empty array, assert not empty
🕗 Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about array/tuple
⏯ Playground Link
https://www.typescriptlang.org/play/?jsx=0&ts=5.7.0-beta#code/C4TwDgpgBAKhDOwoF4oG0B0WCMaC6ANFNnlBAB7AQB2AJvOtkVhrnqQPxTABOArtABcUAGYBDADbwIAbgBQAegVQVAPQ5zNAYwD21RFACW8AKIBbMKBRQAPDAB8ACnLxhMfAEphLow3wp7KABCFwwJGgBzYAALOV19JAkxA1Q7JxdhNBhmLHd2DwCoFzRQ8Ooo6KgAWmI8eTlQSCgAMR0da38AH3RqPjMAIwgeHIxegaH8OrlaCC0knmh4gwyWtvkZubEFqCWkEFcoMcGefE1DEUcg43NLEGd4DwKAbzkVFV2oAC8D1vbUTCw+yISUQ9w8pGSOz0iHkAF8gA
💻 Code
In context:
🙁 Actual behavior
For a homogeneous tuple,
[...T[], T]and[T, ...T[]]are the same thing.TS fails to recognise that in the in-context example: it is saying that
[...ys, last(xs)]could be missing a requirednumberbut it can't becauselast(xs)is the requirednumberthat it is looking for. The only issue is that it is of type[...number[], number]and since[...T[], U]is not the same thing as[T, ...U[]], It chokes.🙂 Expected behavior
Testshould betrueandzs: Fooshould not be a type error.Additional information about the issue
I wish
!isEmptycould narrowT[]to[T, ...T[]].Using a
isNotEmptyhelper withFoodefined asnumber[]would solve this specific case but in the wild it would force me to call it like so!isNotEmpty(xs)in many places, which is terrible for readability.It would be possible in the in-context example to define
Fooas[] | [number, ...number[]] | [...number[], number]andlastas<T>(xs: [T, ...T[]] | [...T[], T]) =>Tbut nobody in the world is doing that and I would be splitting hair alone in my basement not being able to use any library.I think I trip over this more often in recursive logic.