Description
Bug Report
I thought this issue might be related to #46543, but that issue seems to apply to ALL ts versions. This issue is a regression between 4.4.4 and 4.5. In addition to reporting this bug I offer a very simple workaround.
🔎 Search Terms
generic, index, indexed record, type resolution, promise, awaited, workaround
🕗 Version & Regression Information
- This changed between versions 4.4.4 and 4.5
- Edit (3/7/22): Still an issue in 4.5.5 and 4.6.2
Bug started in 4.5 release.
Not fixed in the nightly version as of December 14th 7am ET.
⏯ Playground Link
The example below is purposely contrived in order to express the issue in simple terms.
Playground link with relevant code
💻 Code
export type GenericStructure<
AcceptableKeyType extends string = string
> = Record<AcceptableKeyType, number>;
/**
* structure[key] assignment compiles prior to 4.5, but fails in 4.5.3
*/
async function brokenExample<AcceptableKeyType extends string = string>(structurePromise: Promise<GenericStructure<AcceptableKeyType>>, key: AcceptableKeyType): Promise<void> {
const structure = await structurePromise;
structure[key] = 1;
}
/**
* Possible workaround by explicit typing of 'structure'
*/
async function workAroundExample<AcceptableKeyType extends string = string>(structurePromise: Promise<GenericStructure<AcceptableKeyType>>, key: AcceptableKeyType): Promise<void> {
const structure: GenericStructure<AcceptableKeyType> = await structurePromise;
structure[key] = 1;
}
🙁 Actual behavior
In brokenExample()
:
The type of const structure
is implicitly resolved to Awaited<GenericStructure<AcceptableKeyType>>
Assignment of structure[key] = 1;
results in this error:
Type 'number' is not assignable to type 'Awaited<GenericStructure<AcceptableKeyType>>[AcceptableKeyType]'.(2322)
🙂 Expected behavior
In brokenExample()
:
The type of const structure
should be implicitly resolved to GenericStructure<AcceptableKeyType>
Assignment of structure[key] = 1;
should not result in an error.