Skip to content

Resolution of Record-typed objects that extend string resolve to any #40892

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
alecf opened this issue Oct 2, 2020 · 2 comments
Closed

Resolution of Record-typed objects that extend string resolve to any #40892

alecf opened this issue Oct 2, 2020 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@alecf
Copy link

alecf commented Oct 2, 2020

I'm using a style of nominal typing as described here: https://spin.atomicobject.com/2018/01/15/typescript-flexible-nominal-typing/

But I find when I have a object of type Record<NominalStringType, OtherType>, then resolving a key in that object always resolves to any
TypeScript Version: 4.0.3

Search Terms: Record Flavor Nominal String

Code

interface Flavoring<FlavorT> {
  _type?: FlavorT;
}
type FlavorString<FlavorT> = string & Flavoring<FlavorT>;
type MyString = FlavorString<'MyString'>;

// Create a record type using this nominal type
type RS<T extends string> = Record<T, number>;

// define an instance of this
const r: RS<MyString> = { foo: 1};

// `v` is typed as 'any' even though we know `Record<T, number>[K]` should be a number
const v = r['foo'];

// we can also try with an instance of the nominal type:
const foo: MyString = 'foo';

// `v2` still is type as `any`
const v2 = r[foo];

Expected behavior:

v and v2 above should be typed as a number

Actual behavior:

v and v2 are typed as any

Playground Link:

https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgGIBs4DcD2VQDmAPBtngCoB8yA3gFDLID6YAngA4QD8AXGprijkA3HQC+dNp35koAZTD4QxUoKrIAvMgDOiwsgBkMwYRICKlUVJQBZVgqUFNxvA9MByO2+XvLdOgD0AcgAwlAQcJDIcMjhCHgAJsjWyACu2vpgABbA2sggOAC2oHDoyRwQkhXIAEpyROTIEAAekCAJebqO1Fo1EPFQCQ0ANPmphQBG0H6BwQkQMKAocCDIoLorSMg4MMk52nTxILqxfHVEXnrKPbTIMDg4fACMYqKzyAAGWB9redZJcDy7hWrHcTSwEFW2RwqQIWWQAHcUABrAoIz59AZDcijEDjKZQSgAbQA0gBdH7aLIw9BJKbRMaTaCHHDHMDILDOKBE9z3HDuMlvIKIlAIFbRdDaHDJKCsRHAbLRVbrMCbFA7PYoArFEClcqcHgstl3B58S6OZy8h7uIXBL4AJkpYGA6DKuX1yzyHxBHyNJyw9q5RL5groQA

Related Issues:

This looks possibly related: #36981

@andrewbranch
Copy link
Member

Duplicate of #40193

@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

3 participants