Skip to content

Type check loosening when using Record with union types. #51298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
elementum opened this issue Oct 25, 2022 · 3 comments
Closed

Type check loosening when using Record with union types. #51298

elementum opened this issue Oct 25, 2022 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@elementum
Copy link

elementum commented Oct 25, 2022

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.

@MartinJohns
Copy link
Contributor

Duplicate of #38663.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Oct 25, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@MartinJohns
Copy link
Contributor

Thank you mister typescript-not sir.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants