Skip to content
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

Custom string based type is not allowed to index record #40193

Closed
wclr opened this issue Aug 22, 2020 · 2 comments
Closed

Custom string based type is not allowed to index record #40193

wclr opened this issue Aug 22, 2020 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@wclr
Copy link

wclr commented Aug 22, 2020

TypeScript Version: 3.7.x-dev.201xxxxx

Thre is a MyKeyType to make strings type more distinctive just for typing purposes.

Code

type MyKeyType = string & { __myKey?: true }

// works ok
const indexed: Record<MyKeyType, string> = {} 

const key: MyKeyType = 'myKey'

// Error: Element implicitly has an 'any' type because
// expression of type 'MyKeyType' can't be used to index type 'Record<MyKeyType, string>'
const x = indexed[key]

Expected behavior:

Allow to use the same type for indexing as was used in Record declaration.

Actual behavior:

Error: Element implicitly has an 'any' type because
expression of type 'MyKeyType' can't be used to index type 'Record<MyKeyType, string>'

Also now indexed can not be indexed by "pure" string type.

@MartinJohns
Copy link
Contributor

Duplicate of #37448.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 24, 2020
@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.

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