You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When a type is created with two or more generic parameters (eg. type Context<T, P = any>), the first parameter can be inferred only if the second one isn't provided explicitly (see example).
π Motivating Example
In this example, Test3 doesn't compile, the first parameter cannot be inferred and must be duplicated if the second one is provided:
type Context<T, P = any> = {
value: T
parent: P
}
type DecoratorProps<T, P = any> = {
callback: (context: Context<T, P>) => void
}
function decor<T>(callback: (context: Context<T>) => void) {
return function _(_: any, context: ClassFieldDecoratorContext<unknown, T>) {
const metadata = context.metadata! as Record<PropertyKey, DecoratorProps<T>>
metadata[context.name] = { callback }
}
}
class Test {
@decor((context) => {}) // (parameter) context: Context<string | null, any>. T is correctly inferred
name: string | null = null
}
class Test2 {
@decor((context: Context<string | null, Test>) => {}) // (parameter) context: Context<string | null, Test>, T signature must be duplicated
name: string | null = null
}
class Test3 {
@decor((context: Context<, Test>) => {}) // Doesn't compile: Type expected.
name: string | null = null
}
What do you want to use this for? avoid duplicating types that could be inferred
What shortcomings exist with current approaches? the syntax Context<, Test> is only a possible option, another way to express this type omission would be fine.
What workarounds are you using in the meantime? provide the first parameter like in Test2
The text was updated successfully, but these errors were encountered:
π Search Terms
generic parameters, type inference
β Viability Checklist
β Suggestion
When a type is created with two or more generic parameters (eg.
type Context<T, P = any>
), the first parameter can be inferred only if the second one isn't provided explicitly (see example).π Motivating Example
In this example, Test3 doesn't compile, the first parameter cannot be inferred and must be duplicated if the second one is provided:
See playground here.
π» Use Cases
Context<, Test>
is only a possible option, another way to express this type omission would be fine.Test2
The text was updated successfully, but these errors were encountered: