You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
string: value interface not assignable to Record<string,unknown> #50087
interfaceOldProps{name: unknown;}typeNewProps=Record<string,unknown>;// or// type NewProps = { [key:string]:unknown };functiontest(oldProps:OldProps){constprops:NewProps=oldProps;returnprops;}
🙁 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).
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.
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 😄
Activity
jcalz commentedon 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 commentedon Jul 29, 2022
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 commentedon Aug 29, 2022
Joe Calzaretta (@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:
If this has been a breaking change since 3.5 I'm at a loss as to why this would be happening now?