-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Generic typing inference composition work as variable not as function #30808
Comments
Btw, everything work perfectly when I set explicitly all generic parameters: const target = map<DestPropsInterface, Partial<MatchKeys<SrcPropsInterface, DestPropsInterface>>>(obj, {
srcAttributeNumber: 'anotherAttributesNumber',
srcAttributeString: 'destAttributesString',
foo: 'bar' // Error as expected
}); |
There's a lot going on here I don't understand which part(s) you consider to be the "bug" here - can you simplify this down into a basic example of a small amount of setup + what code you'd expect to error/not error? |
Ok, I will try to simplify and to isolate which part of the code creates the bug. |
function map<T, P extends Partial<T>>(data: T, map: P) {
return {};
} You don't get suggestions here because |
It's vexing. The language service is asking the checker what the type of the surrounding object literal is in order to show completions. The valid and correct answer is Basically we'd need an entirely different kind of operation here where the language service would ask the checker, What property values would be valid here?, which almost requires a sort of counterfactual reasoning that we're really not capable of. For the specific case of |
Sorry, it wasn't my intention ^^" I don't really know how exactly the language service works under the hood. I'm trying a different approach that provide us all what we need. So it's absolutely not a priority in comparison of what you already have in the roadmap 🙂 Especially when I see the evolution of TypeScript since I started to use it in 2014, it's absolutely fantastic, and I start all my project with it 😀 Keep continue like that, and we can close this issue if you want 👍 |
I think I found another instance of this issue. Check out this TypeScript Playground /**
* Set a value in an object, inferring the path from the value attempting to be passed.
* When passing "", the generic is resolved as `string` and thus can never match the `age` in Person.
*/
type SetFieldValueFn<Values> = <Value,>(
name: PathLikeValue<Value, Values>,
value: Value,
) => void;
interface Person {
age: number | "";
}
const setFieldValue: SetFieldValueFn<Person> = (name, value) => {}
// this is resolving to `setFieldValue<string>("age", "");`,
// instead of `setFieldValue<"">("age", "");`
setFieldValue("age", "");
// manually typing it works fine
setFieldValue<"">("age", "");
// the rest works
setFieldValue("age", 1);
setFieldValue("age", "" as "");
setFieldValue("age", 1 as number | "");
setFieldValue<number | "">("age", 1 as number | "");
setFieldValue<number | "">("age", 1);
setFieldValue<number | "">("age", "");
setFieldValue<number | "">("age", "");
setFieldValue<number>("age", 1); "why are you doing this backwards?", please accept my apologies but this is the way. this way allows a previously typed setFieldValue function to autosuggest names matching values to devs as they type. |
TypeScript Version: 3.4.2
Search Terms:
Generic variable vs generic function
Generic composition
Generic inference parameter in function
Code
Let's consider this code
In a variable approach, the following code works perfectly with all completion:
With a generic function approach, we get some weird things:
Case 1: no completion anymore
But as soon as I write blindly a known attribute from
SrcPropsInterface
, the type checking work:Case 2: any-like
We can put any kind of value and there is no complaint (and naturally no completion):
Case 3: no output anymore
As soon as the
map
object is correctly fill, thetarget
object get correctly all remaining attribute:But if we provide a wrong map because the type checker doesn't provide the correct feedback,
target
is converted asnever
:Expected behavior:
Case 1: get correct type checker in the
map
parameters like for thevariable
approachCase 2: I suppose it's just a side-effect of Case 1, but I expect to have TypeScript wiping me by attempting to introduce unknown parameters.
Case 3: I suppose it's just a side-effect of Case 1 and 2.
Actual behavior:
Check Case 1, Case 2 and Case 3: no type checking on the generic
map
parameter whereas in avariable
approach, it works.Playground Link:
Playground @ 2019-04-08
Related Issues:
#28938
The text was updated successfully, but these errors were encountered: