You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Type 'keyof T' cannot be used to index type 'T extends <type>' with unions in v4.4.4 and newer #46495
This one was kinda hard to find search terms for, I mainly look at changes between v.4.3.5 and v.4.4.4 in the changelog and could not find anything that could have potentially caused this.
🕗 Version & Regression Information
This changed between versions v4.3.5 and v4.4.4. It works fine on TS Playground on v4.3.5 and earlier but errors on v4.4.4 and later.
Same as TS Playground link minus workaround comments:
interfaceButton{type: "button";text: string;}interfaceCheckbox{type: "checkbox";isChecked: boolean;}typeControl=Button|Checkbox;functionupdate<TextendsControl,KextendskeyofT>(control : T|undefined,key: K,value: T[K]): void{if(control!==undefined){control[key]=value;// this breaks on v4.4.4 but works on v4.3.5}}
🙁 Actual behavior
When running the code above in v4.4.4 and later versions it errors on the last line (control[key] = value;) with:
Type 'K' cannot be used to index type 'Control'.
When transpiling the code on v4.3.5 and earlier it works fine.
Both these two workarounds work on all versions:
(control)[key]=value;// add parentheses
constc=control;// use inline variablec[key]=value;
It seems like control is resolved to type Control in the bugged snippet but resolves to T extends Control in the other snippets.
Also sidesnotes: the error goes away if | undefined is removed from the parameter type, and also if T is changed to something that is not an union.
🙂 Expected behavior
I would expect control would resolve to T extends Control in all 3 snippets of the code.
changed the title [-]Type 'keyof T' cannot be used to index type 'T extends <Class>' with unions in v4.4.4 and newer[/-][+]Type 'keyof T' cannot be used to index type 'T extends <type>' with unions in v4.4.4 and newer[/+]on Oct 23, 2021
This is an effect of #43183 and the subsequent revision in #44621. The revision needs to be slightly less conservative. I'm surprised no one has run into this until now.
Activity
[-]Type 'keyof T' cannot be used to index type 'T extends <Class>' with unions in v4.4.4 and newer[/-][+]Type 'keyof T' cannot be used to index type 'T extends <type>' with unions in v4.4.4 and newer[/+]ahejlsberg commentedon Oct 25, 2021
This is an effect of #43183 and the subsequent revision in #44621. The revision needs to be slightly less conservative. I'm surprised no one has run into this until now.