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
Cannot index into generic type if unions involved #23618
Search Terms: typescript intersection union "cannot be used to index"
Code
// This works:typeDBBoolTable<Kextendsstring>={[kinK]: 0|1}enumFlag{FLAG_1="flag_1",FLAG_2="flag_2"}typeSimpleDBRecord<Flagextendsstring>={staticField: number}&DBBoolTable<Flag>functiongetFlagsFromSimpleRecord<Flagextendsstring>(record: SimpleDBRecord<Flag>,flags: Flag[]){// stubreturnrecord[flags[0]]}// This fails:typeDynamicDBRecord<Flagextendsstring>=({dynamicField: number}|{dynamicField: string})&DBBoolTable<Flag>functiongetFlagsFromDynamicRecord<Flagextendsstring>(record: DynamicDBRecord<Flag>,flags: Flag[]){// stubreturnrecord[flags[0]]// Error: Type 'Flag' cannot be used to index type 'DynamicDBRecord<Flag>'}
I know in this example I can just say dynamicField: number | string, but in my actual code there's a more complex type than just one field like that (and contains outside constraints like DB schema).
Expected behavior:
I should be able to index into the db record in the second example even with that dynamic field, because the DBRecord type says it should have all the flags in it.
Actual behavior:
It is unhappy with indexing into the type with a union in it, with the error mentioned above.
Activity