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

"Index signature missing" error when using Interface vs Inline Type Definition #46715

Closed
brwarner opened this issue Nov 7, 2021 · 2 comments
Closed

Comments

@brwarner
Copy link

brwarner commented Nov 7, 2021

Bug Report

πŸ•— Version & Regression Information

Typescript version 4.4.4 (also tested on the nightly v4.6.0-dev.20211105)

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

declare const $CombinedState: unique symbol

// Declare combined state as an union of a symbol index and the template parameter S
type CombinedStateWorks<S> = {
    readonly [$CombinedState]?: undefined
} & S;

// Try to assign a combined state to a Record<string, unknown> ... success! no compile errors
{
  const combined: CombinedStateWorks<{a: number}> = { a: 2 };
  const record: Record<string, unknown> = combined;
}

// Declare combined state as an union of the interface "EmptyObject" (identical to the inline type above) and the template parameter S
interface EmptyObject {
    readonly [$CombinedState]?: undefined
}
type CombinedStateFails<S> = EmptyObject & S;

// Try to assign a combined state to a Record<string, unknown> ... fail!
{
  const combined: CombinedStateFails<{a: number}> = { a: 2 };
  // ERROR RIGHT HERE
  const record: Record<string, unknown> = combined;
}

πŸ™ Actual behavior

Despite CombinedStateFails and CombinedStateWorks being the same type specification (except one uses an interface, one an inline type definition), one fails to case to a Record<string, unknown> and the other succeeds.

πŸ™‚ Expected behavior

Both cases should have the same compilation result (success).

This is a problem because recent releases of Redux have refactored their CombinedState type from the first example here to the second, breaking some of my code. I'm not sure what type besides Record<string, unknown> I can use as my parameter type now.

@MartinJohns
Copy link
Contributor

type and interface are not interchangeably. This is working as intended. See #42825 (comment).

@brwarner
Copy link
Author

brwarner commented Nov 7, 2021

Thanks. Guess I'll need to find a different way to do this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants