-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Call to function returning non-nullable T
with contextual type U | undefined
should infer T = U
#26119
Comments
This happens with any union: declare function acceptValue(value: number | string): void;
declare function getValue<T extends number | boolean>(): T;
acceptValue(getValue());
|
ref #16072 |
As Andy identified this doesn't really have to do with When we have an "outside" inference candidate (in this case, the contextual type that related to the return type), the right thing to do would be do intersect (set intersection, not |
Discussed a bunch... TL;DR the declaration Logged #26129 |
That was just a minimal example. Here's an example where the function is implementable but the variable still can't be inferred from its arguments but only from the return type: enum Axis { ROW = "row", COL = "col" };
function getMappedType<R extends { [A in Axis]: unknown }>(
row: R[Axis.ROW], col: R[Axis.COL]): R | Pick<R, Axis> {
return { [Axis.ROW]: row, [Axis.COL]: col};
}
function acceptMappedType(mappedType?: {[A in Axis]: number}) { }
acceptMappedType(getMappedType(42, 43)); Playground (remember to enable strictNullChecks) This is from my spreadsheet project that is pushing mapped types to the limit to keep track of row vs. column IDs (see also #25879). |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Search Terms
infer undefined constraint null return contextual type strictNullChecks
Conceivably related: #16943
Suggestion
With
strictNullChecks
, when calling a function whose return type is a type variableT
with a non-nullable constraint, and the contextual type includesnull
and/orundefined
, TypeScript should generate an inference forT
of the non-nullable part of the contextual type. Currently, I am getting no inference at all.Minimal example (with
strictNullChecks
):Playground
Use Cases
This came up when I attempted to enable
strictNullChecks
on this code.spanStyles
is an optional property of the contextual type, the return type ofPA
is a type variable with a non-nullable constraint, and it's not possible to infer the return type from the arguments toPA
, so I got an error. The error is easily worked around by specifying the type argument toPA
.Examples
I think the suggestion is sufficiently specific that it doesn't need more examples.
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: