-
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 type argument cannot be inferred by a callback parameter return type if the same type was used for the callback's parameter #9659
Comments
I believe the problem here is we have two inference sites for There might be some fix here without other side effects but I don't know what it would be. |
So should we create a proposal to fix that? |
It would be very helpful! |
Just imagine how convenient it was to write this (for example - Promise executor): const promise = new Promise((resolve, reject) => {
resolve(123);
});
promise.then((res) => {
console.log('I get called:', res.toFixed());
}); Also disappear errors like this: const promise = new Promise((resolve, reject) => {
resolve(123);
resolve("!");
resolve(true);
}); But now we have... const promise = new Promise<number>((resolve, reject) => {
resolve(123);
});
//or
const promise: Promise<number> = new Promise((resolve, reject) => {
resolve(123);
});
// more example
function doSome(): Promise<number> { // let's say that do Some logic is very complicated and we want to explicitly specify the results
return new Promise<number>((resolve, reject) => { // <number> for greater security logic inside Promise executor
resolve(123);
});
} Many similar problems will go into oblivion if this is done |
@Igorbek my bad |
See this. type AABB = 'AA' | 'BB';
let arr: any[];
let arr2: AABB[] = arr.map(v => 'AA');
let arr3: AABB[] = arr.map(v => 'BB'); If I want it work, I could only use this: let arr2: AABB[] = arr.map(v => 'AA' as 'AA');
|
I think the behavior today is correct. If you write a(() => ({ x: 1 })); Then If the callback does take a parameter, then this is a contravariant-only inference case, and |
TypeScript Version: 1.8.10
Code
Expected behavior:
In
a(x => ({ x: 1 }))
,T
is inferred as{ x: number }
Actual behavior:
In
a(x => ({ x: 1 }))
,T
is inferred as{ }
The text was updated successfully, but these errors were encountered: