Skip to content

Array slice infer patterns #181

@kaleidawave

Description

@kaleidawave

The following code should type-check fine

type Head<T> = T extends [infer H, ...Array<any>] ? H : never;

type Tail<T> = T extends [any, ...infer Tail] ? Tail : [];

1 satisfies Head<[1, 2, 3, 4, 5]>;
[2, 3, 4, 5] satisfies Tail<[1, 2, 3, 4, 5]>;

There is some current problems with representing spread arrays patterns. It isn't great as it reuses the same object helper as regular expression space array literals (and so doesn't create an anonymous object literal type). Maybe this could be handled regularly, maybe there needs to be a special fn extract_as_slice helper method?

Metadata

Metadata

Assignees

Labels

needs-investigationFurther information is requestedsubtypingRelated to comparing two typestsc-behaviourTrying to match TSC behaviour

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions