-
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
Type inference problem when using inline vs declared generator function #60382
Comments
With |
This one is tricky and falls under #47599 umbrella. The problem is that inference is performed in 2 passes when context-sensitive functions are involved. The first one replaces such functions with "any function type" and blocks inference from it. The problem is that the signature inferred from the first pass has to pass So what happens here after this second pass that the third param type gets computed as The reason why we get this param type is that it's derived from constraints (in absence of legit inference candidates and at this point - as I mentioned - your context-senstivie functions were ignored). The reason why it works when it's inlined is that in that scenario it's also context-sensitive so it's also excluded. So the inference is OK with the signature inferred from the first pass and moves to the second. The very minimal (even if nonsensical π ) repro: interface ReadableStream {
readable: boolean;
read(size?: number): string;
}
type PipelineTransform<S, U> = ReadableStream | ((source: S) => U);
declare function pipeline<
A extends Iterable<any>,
T1 extends PipelineTransform<A, any>,
T2 extends PipelineTransform<T1, any>,
>(source: A, transform1: T1, transform2: T2): void;
export async function main() {
const input = [1, 2, 3, 4];
pipeline(
input,
(source) => source,
(source) => {},
);
}
export async function mainTwo() {
const input = [1, 2, 3, 4];
const identity = (source: number[]) => source;
pipeline(
input,
(source) => source,
identity, // error π’
);
} |
Wow, thank you for the explanation! What's even more fascinating is that if I tweak the const addOne = async function* <T extends number>(
source: AsyncIterable<T>,
): AsyncGenerator<number, void, any> {
for await (const chunk of source) {
yield chunk + 1;
}
}; |
Yee, IIRC the generic functions are also skipped by the first pass - although perhaps that's done conditionally. |
The inference seems weak though, e.g. the output type |
This issue has been marked as "Design Limitation" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
π Search Terms
async generator inference pipeline anonymous
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?target=99&ts=5.6.3#code/JYWwDg9gTgLgBAbzmYYCmAbYA7NcC+cAZlBCHAOTYQAmaAXAM4xRoCGIA9GKSMI2kYUA3AChRbRgE9sAY2IBXOTGARscEGxwAKAJSJRcOLLXM4OMAvgBeOAG0AjABo4AJhcBmFwBYAumMM4NgB3LXgUdCxcbUCjCysnWKDpOUVlVWwAKjhtRggFKFk0fQQkoyJoINDgeG0TbDNZAAslAGs4CCI4PIKikrKjOClgTBpjFux27NcxQcH8JPxEuckZeSIlWRU1bNz8wuKDOfLKkLCc+saJ9s7u-b6j46Nh0fG2uABqOAdZ44W5pZJVapDbpNQ5HoHfpPCpQKrnOqmeDNd63SEPUpPIyXCAYNAAOgwEAA5toKAAxHBsDBwbAKEAAIzQUCELhRk10vwBi2WcE5ogWEhS60223UmhwABVghA9I9LvB4jZ7M43J4fP5xNikUEaDQAPK4OC2YEisFZHJJdEMOAAQWFAEkYMy2Ay8QAeOmM5kAPl5unoduFAHE0LgoGwYNBPfSmVAXAA3CDAGguNjYKQ+x4nOFnGoXHXsm5da3Q44vDBjIufb5cgiBfABIx58KoTA4NAxOZK3nN4VpLYZXal7ODWHw-OIhrI64dEv3Q6YrEVquz6Z1oz-ea93UG3A700DsUQhdlubjlsF6dvSZzu69RcDbUNXEEomkilUmleuOsm+tfkni3Td-TEQUJWwPQxAg6VZX5IA
π» Code
π Actual behavior
When inlining the
addOne
function (inmain
), the type of the rest of the pipeline is inferred.Type of function:
Type of argument in the next function:
When extracting
addOne
(inmainTwo
), even matching the exact same types, the compiler is unable to infer the type of the next async iterable and you get an error for implicitany
.Type of function (exact same as before π€ )
Type of argument in the next function not inferred:
π Expected behavior
The second example should behave exactly as the first, and
source
should be inferred.Additional information about the issue
I was hesitant to file this issue given the possibility something is wrong with
@types/node
, but this seems to be beyond the typings because the typings shouldn't know/care whether or not I declare the function inline or reference it.The text was updated successfully, but these errors were encountered: