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

string: value interface not assignable to Record<string,unknown> #50087

Closed
mikeybinns opened this issue Jul 28, 2022 · 3 comments
Closed

string: value interface not assignable to Record<string,unknown> #50087

mikeybinns opened this issue Jul 28, 2022 · 3 comments

Comments

@mikeybinns
Copy link

Bug Report

🔎 Search Terms

unknown record
Index signature

🕗 Version & Regression Information

Bug initially found in v4.7.4
Oldest found version with bug v3.5.1 in typescript playground
Bug found in nightly version v4.8.0-dev.20220728

  • This changed between versions v3.3.3 and v3.5.1

⏯ Playground Link

Playground link with relevant code

💻 Code

interface OldProps {
  name: unknown;
}

type NewProps = Record<string, unknown>;
// or
// type NewProps = { [key:string]:unknown };

function test(oldProps:OldProps) {
  const props:NewProps = oldProps;
  return props;
}

🙁 Actual behavior

OldProps type is not assignable to NewProps.
Typescript returns the following error:

Type 'OldProps' is not assignable to type 'NewProps'.
  Index signature for type 'string' is missing in type 'OldProps'

Error is consistent with old style record syntax.
Error is consistent if OldProps["name"] type is string or any
Error not found if NewProps = Record<string, any>;
Error also not found if [key:string]:[whatever type] is added to OldProps (triggers different expected error if type conflicts with name).

🙂 Expected behavior

OldProps type is assignable to NewProps.

@jcalz
Copy link
Contributor

jcalz commented Jul 29, 2022

This is working as intended. Interface types don't get implicit index signatures as per #15300. The behavior where {[k: string]: unknown} is treated more strictly than {[k: string]: any} was implemented in #30637, released with TS3.5, and was a documented breaking change.

@mikeybinns
Copy link
Author

Okay, looks like for my use case, I should be using a type alias instead of an Interface, and (i cut this from the reproduction) instead of extending the interface, I need to intersect two type aliases.
Thanks for your help 😄

@the0rem
Copy link

the0rem commented Aug 29, 2022

@jcalz we're trying to upgrade from typescript 4.3.5 to 4.6.4 and are now receiving this error.

Previously an interface like below would allow an interface to be passed in as the generic:

export interface IIntegration<
  T extends Record<symbol, unknown> = Record<symbol, unknown>
> {
  type: IntegrationType;
  data: T;
}

If this has been a breaking change since 3.5 I'm at a loss as to why this would be happening now?

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

3 participants