-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type inference issue when using generic function and generic constraint #24747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This works as expected. |
@RyanCavanaugh Okay, maybe I oversimplified the repro sample. I updated it (now indeed using The real code is a fluent API, where I need the type inference and the |
@RyanCavanaugh Also added |
@RyanCavanaugh Any news on this? I have now a second but much easier sample using a conditional type instead of a generic constraint: type GenericFunc<TA, TB> = (x: TA) => TB;
class GenericFuncHolder<TA, TB> {
constructor(readonly func: GenericFunc<TA, TB>) { }
}
function wrap<TA, TB>(func: GenericFunc<TA, TB>) { return new GenericFuncHolder(func); }
//
export type PropertyToTypeName<TProperty> =
TProperty extends string | undefined ? "text" :
TProperty extends number | undefined ? "number" :
never;
function withFunc<TProperty>(
func: GenericFunc<string, TProperty>, typeName: PropertyToTypeName<TProperty>) {
console.log('func points to', typeName);
}
function withHolder<TProperty>(
holder: GenericFuncHolder<string, TProperty>, typeName: PropertyToTypeName<TProperty>) {
console.log('holder points to', typeName);
}
withHolder(wrap(x => x.length), "number"); // works fine
// emits TS2345: Argument of type '"number"' is not assignable to parameter of type 'never'
withFunc(x => x.length, "number"); ... like in the sample above the problem is that in the |
One more observation: When adding |
I feel like I'm missing something, because this example really shouldn't error, but has since 2.8 type TypeName<T> = T extends number ? "number" : "something else";
function fn<T>(func: (x: string) => T, p: TypeName<T>): void { }
// Error
fn(x => x.length, "number"); |
The sample above now produces a crash in the LS in the Playground...
|
TypeScript Version: 3.0.0-dev.20180607 and 2.9.1
Search Terms: type inference generic constraint function callback accessor
Code
(Sorry for the long repro, it's the simplified version of a production issue and the inference issue seems to be a very special case.)
Expected behavior:
Type inference works for
funcParamsWithConstraint_BAD
as it works forholderParams
andfuncParamsWithoutConstraint
. It seems that the generic constraint ofTY2
narrowsTX2
to{}
, but this works when usingwrap()
on the call site. Which is the reason, why I would expectfuncParamsWithConstraint_BAD
to work just fine.Actual behavior:
"TS2339: Property 'xProperty' does not exist on type '{}'" in the last line.
Playground Link: Playground Link
EDIT: Extended repro to make it more realistic.
The text was updated successfully, but these errors were encountered: