Explicit target type breaks generic parameter inference for function call with intersection return type #61467
Labels
Help Wanted
You can do this
Possible Improvement
The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone
🔎 Search Terms
explicit target type breaks generic parameter inference function call intersection return type contextual typing
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play/?strictBuiltinIteratorReturn=true#code/GYVwdgxgLglg9mABMAjAHgIKIKYA8rZgAmAzogNbYCecwiAEgCoCyAMgKIA22AtoVIwCGAcwBygvs0EAHADSIAQgD4AFIIBciDPIBGmhQEpNTNl178hYidinSA2hgC6iAGSLEAbwCwAKESIAJ2woEACkAHkdACtsaAA6QRISGGEwFSI4CBA+MCg4iCDBAjMcqDUDXQMAbl8AX19QSFgEZAAmTBx8QlJPWvllNU1tRD1FIy1Xd28-QODQiOjYvMTk1LVKmp96n18Ael3ERgALGDJgQRhOMh1YwRASbEOAZUQYMGBsALIFRABeBhYrAUICgUAQJX4kw8CAgnBgEHImhUBj+SkQADc4DAiLVXmASARBERELR3P9oZA4QikSjfmjMdjthAEATEAQCShjIDgaDwdxSlCYVTEYhkaiMVicX9kCgVAAiHQgsFgOXyCmw+EixrQeBpAweWq1ap7A7HU6IADucAC5DI2HRhDZRzgIGERydjzeHwCQWJUCo0k9ZCgR0eJGsiESHs60ipMCgbIDj0EOjgDt8zPxCfZUFa-nzBcLReLJdL-1Q8sVvJVaqFms02uaeoNRs2+0OJzIVpt11u90ewkIn3hiGkggC1gIX0jQUQwhgDqQeFj8PjnCoGZZ2ewBIAzFy2DzlRDcoLKfXRbT6ZLceX0AqlQhVZ469TL+KGTjVA-q8-1cKG3AHUEGRFtjR8dszS7a1bRGPsHhnAI4AtZAgKbMhx0eMA4ATQRgA+aBsGJYBrUQEg4D4WZEgQTcszZHcoAAFgPIFHzAE8Ezcf8LzFOkJUZaUKx-ZU-1fEVeLRMC21NTtLRg3sIDuBCvU+QgIEebtYJuUFPkteNnRBRNA0QHgZFo1kcwAVhYo8+XMU8uLEmkPxvQTWnSTJsn4fJCmKfl+ErNi5QqF9zzfRtdVAw1wKAA
💻 Code
🙁 Actual behavior
test1
shows an error:This is because TS sees that
test1
is explicitly typedHTMLButtonElement & {onclick: () => void}
, and so it erroneously infers the generic type parameters off1
asB = HTMLButtonElement & {onclick: () => void}
, even though the correct inference isB = {onclick: () => void}
.More interestingly,
B
is inferred correctly if the explicit target type is omitted, as demonstrated withtest2
. However, the inferred type oftest2
is the same as the explicit type oftest1
!🙂 Expected behavior
If a program is valid with an inferred type, then it should also be valid when that type is turned explicit.
Additional information about the issue
The code snippet also demonstrates that this issue is somehow related to using mapped types and
function(){}
instead of() => {}
. While curious, I think the main issue is thattest1
andtest2
behave differently.The text was updated successfully, but these errors were encountered: