Skip to content

Empty tuple type now meaningful (at least, in unions) #13126

Closed
@krryan

Description

@krryan

I have a situation where the only valid states for a property are [], [A], or [A, B, B], so I would like to define that property as [] | [A] | [A, B, B]. But this gives the error "A tuple element list cannot be empty," and indeed I see that there were a few prior issues relating to the compiler having (incorrectly) allowed it in the past. It seems to me that now, between overloading and unions, this type is useful. Also useful in a typeguard: isEmpty<A>(values: A[]): values is [] { return values.length === 0; }

I suppose the most difficult part of it would be actually tracking that a given array is empty, so that it could be matched against that definition.

Per the checklist, the grammar is simple enough, [] in type positions, shouldn't have any issues with respect to JavaScript. I don't foresee any semantic issues except the above bit about actually recognizing empty arrays as such. The emit isn't affected at all, and compatibility shouldn't be an issue. I'm not rightly sure about the concerns raised in the Other section.

Metadata

Metadata

Assignees

Labels

FixedA PR has been merged for this issueSuggestionAn idea for TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions