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

Type inference of dynamic properties #42854

Closed
Nctdt opened this issue Feb 18, 2021 · 2 comments
Closed

Type inference of dynamic properties #42854

Nctdt opened this issue Feb 18, 2021 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@Nctdt
Copy link

Nctdt commented Feb 18, 2021

Bug Report

πŸ”Ž Search Terms

union type, dynamic properties

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

declare const bar: 'name' | 'age'
const obj = {
  any: 'any',
  [bar]: 'wow',
}

πŸ™ Actual behavior

const obj: {
    [x: string]: string;
    any: string;
}

πŸ™‚ Expected behavior

const obj: {
    name: string;
    any: string;
} | {
    age: string;
    any: string;
}

I think this is a predictable type.

@Nctdt
Copy link
Author

Nctdt commented Feb 18, 2021

I currently use type assertions.
Playground
The test is as follows:

declare const bar: 'name' | 'age'
declare const baz: 'my' | 'test'
const obj = {
  any: 'any',
  [bar]: 'bar',
  [baz]: 'baz',
} as C2
type T1<O, T, V> = O extends any
  ? T extends string | number | symbol
    ? {
        [C in T | keyof O]: C extends keyof O ? O[C] : V
      }
    : never
  : never
type C1 = T1<{ any: string }, typeof bar, string>
type C2 = T1<C1, typeof baz, string>

@jcalz
Copy link
Contributor

jcalz commented Feb 18, 2021

Duplicate #13948, #21030

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 18, 2021
@Nctdt Nctdt closed this as completed Feb 19, 2021
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