Description
Suggestion
π Search Terms
anonymous, type parameter, type parameters
β Viability Checklist
My suggestion meets these guidelines:
- [*] This wouldn't be a breaking change in existing TypeScript/JavaScript code
- [*] This wouldn't change the runtime behavior of existing JavaScript code
- [*] This could be implemented without emitting different JS based on the types of the expressions
- [*] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- [*] This feature would agree with the rest of TypeScript's Design Goals.
β Suggestion
Whenever one wants to apply a generic type constraint, a full type must be provided.
The suggestion is that in a type/function definition Typescript could accept a generic type constraint for the property/argument, without having to declare a type parameter
π Motivating Example
interface Person<Profession extends Carpenter | Hacker | Judge> {
name: Exclude<extends string, "">;
profession: Profession;
employer?: Profession extends Hacker | Judge ? Person : never
}
const divide = (a: number, b: Exclude<extends number, 0>) => a/b;
const ping = (ip: extends `${number}.${number}.${number}.${number}` ? string : never) => {}
const definitelyReturnsTrue = (arg: Exclude<,undefined | null | false | "" | 0>) => !!arg;
π» Use Cases
When in a type/function definition, if wanting to use a generic constraint for a parameter/argument, one must declare a type parameter even though it doesn't get used anywhere else in the definition.
Personally I see myself using such a feature most when I'm writing complex functions that already have multiple type parameters declared, and while I really want to constrain a certain parameter (my most common use case is excluding "" from string arguments but I've had other uses too), I might forgo doing so, because adding another type parameter to the function significantly increases its' complexity (as I now have to mentally keep track of another parameter, which ends up only being used once).
The way I see it, adding a type parameter in the beginning of the function's definition means that parameter is meant to be used in several places within that function, like the Person interface's Profession example. If it's only used once, I generally shouldn't need a type parameter.
In the meantime, of course, I either add an extra type parameter which I'll only ever use once, or if I decide it's not worth the complexity, I give up on the desired type safety and go with the strictest non-generic constraint available to me (usually a primitive like string or number).
To be clear, I do assume that if using the function/interface's type, rather than calling it/constraining a new object literal, it will count as generic and require type parameters, as the language obviously can't infer the generic constraint's type parameter in that case. Typescript could maybe name the anonymous type parameter based on the argument/parameter it's used in?