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

Overloaded indexed type isn't initializable with an object literal #50575

Closed
indutny-signal opened this issue Aug 31, 2022 · 2 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@indutny-signal
Copy link

Bug Report

πŸ”Ž Search Terms

"Overloaded indexed type"

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about it

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type X = {
   [key: string]: { a: number; }
} & {
    b: { b: number};
}

let x: X = {
    a: { a: 1 },
    b: { b: 1 },
}

πŸ™ Actual behavior

Type error:

Type '{ a: { a: number; }; b: { b: number; }; }' is not assignable to type 'X'.
  Type '{ a: { a: number; }; b: { b: number; }; }' is not assignable to type '{ [key: string]: { a: number; }; }'.
    Property 'b' is incompatible with index signature.
      Property 'a' is missing in type '{ b: number; }' but required in type '{ a: number; }'.

πŸ™‚ Expected behavior

No type error

@jcalz
Copy link
Contributor

jcalz commented Aug 31, 2022

The term "overloaded indexed type" doesn't really convey what you're doing. The behavior here is working as intended.

It looks like you're trying to write a "rest index signature" type as requested in #17867 (and mentioned in SO here), and unfortunately there's not really any great way to do it. An intersection is okay-ish to read from but you can't easily initialize it, as you can see.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Aug 31, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' 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
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants