Closed
Description
Bug Report
Under certain conditions typescript compiler allows setting any value for a record that has union type key. See code section below.
π Version & Regression Information
Tested in 4.8.4
β― Playground Link
Playground link with relevant code
π» Code
type UnionType = "ABC" | "XYZ"
type RecordType = Partial<Record<UnionType, string>>
// no errors, ok
const value1: RecordType = {
XYZ: "some value",
}
// error, ok, number where string is expected
const value2: RecordType = {
XYZ: 123,
}
// error, ok, number where string is expected
const value3: RecordType = {
["XYZ"]: 123
}
// error, ok, key test is not allowed
const value5: RecordType = {
["test"]: 123
}
// as UnionType clause is important
// it could be a function that returns UnionType, e.g. const key = getUnionKey(), same problem
const key: UnionType = "XYZ" as UnionType
// no error, but it should be there
const value4: RecordType = {
[key]: 123
}
π Actual behavior
No error when using square brackets. Last example.
π Expected behavior
It should throw an error.