IssueRaiser

Feature Request: "extends oneof" generic constraint; allows for narrowing type parameters

## Search Terms * generic bounds * narrow generics * extends oneof <!-- List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily --> ## Suggestion Add a new kind of generic type bound, similar to `T extends C` but of the form `T extends oneof(A, B, C)`. (Please bikeshed the semantics, not the syntax. I know this version is not great to write, but it *is* backwards compatible.) Similar to `T extends C`, when the type parameter is determined (either explicitly or through inference), the compiler would check that the constraint holds. `T extends oneof(A, B, C)` means that *at least one of* `T extends A`, `T extends B`, `T extends C` holds. So, for example, in a function ```ts function smallest<T extends oneof(string, number)>(x: T[]): T { if (x.length == 0) { throw new Error('empty'); } return x.slice(0).sort()[0]; } ``` Just like today, these would be legal: ```ts smallest<number>([1, 2, 3); // legal smallest<string>(["a", "b", "c"]); // legal smallest([1, 2, 3]); // legal smallest(["a", "b", "c"]); // legal ``` But (unlike using `extends`) the following would be **illegal**: ```ts smallest<string | number>(["a", "b", "c"]); // illegal // string|number does not extend string // string|number does not extend number // Therefore, string|number is not "in" string|number, so the call fails (at compile time). // Similarly, these are illegal: sm

Posted by Nathan-Fenner on Oct 11, 2018, 8:49 PM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.4,000 issues0 comments

reliability (Software)

Feature Request: "extends oneof" generic constraint; allows for narrowing type parameters

Nathan-FennerOct 11, 2018, 8:49 PM

## Search Terms * generic bounds * narrow generics * extends oneof <!-- List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily --> ## Suggestion Add a new kind of generic type bound, similar to `T extends C` but of the form `T extends oneof(A, B, C)`. (Please bikeshed the semantics, not the syntax. I know this version is not great to write, but it *is* backwards compatible.) Similar to `T extends C`, when the type parameter is determined (either explicitly or through inference), the compiler would check that the constraint holds. `T extends oneof(A, B, C)` means that *at least one of* `T extends A`, `T extends B`, `T extends C` holds. So, for example, in a function ```ts function smallest<T extends oneof(string, number)>(x: T[]): T { if (x.length == 0) { throw new Error('empty'); } return x.slice(0).sort()[0]; } ``` Just like today, these would be legal: ```ts smallest<number>([1, 2, 3); // legal smallest<string>(["a", "b", "c"]); // legal smallest([1, 2, 3]); // legal smallest(["a", "b", "c"]); // legal ``` But (unlike using `extends`) the following would be **illegal**: ```ts smallest<string | number>(["a", "b", "c"]); // illegal // string|number does not extend string // string|number does not extend number // Therefore, string|number is not "in" string|number, so the call fails (at compile time). // Similarly, these are illegal: sm

Comments (0)

No comments yet. Start the discussion.

Comment on this issue