TS 4.1+ Key remapping in mapped types allows bypassing statically known members check
See original GitHub issueBug Report
🔎 Search Terms
key remapping static member, arbitrary interface keys, bypassing interface dynamic member check
🕗 Version & Regression Information
With the key remapping introduced in TS 4.1 for mapped types, the following error can be bypassed:
An interface can only extend an object type or intersection of object types with statically known members. (2312)
I was unable to test this on prior versions because key remapping didn’t exist.
⏯ Playground Link
💻 Code
// @ts-expect-error -- this isn't allowed
interface RecordInterface<K extends keyof any, V> extends Record<K, V> { }
type RemapRecord<K extends keyof any, V> = { [_ in never as K]: V }
// @ts-expect-error -- but this is!
interface RecordInterface<K extends keyof any, V> extends RemapRecord<K, V> { }
🙁 Actual behavior
TypeScript inconsistently prevents using interfaces with dynamic members.
🙂 Expected behavior
Either RecordInterface
to have an error, or to be able to replace Record1
with Record
without any error.
CC: @tjjfvi, who discovered this, but hasn’t submitted a bug.
There is a feature request (#41383) about changing the definition of Pick
which includes a comment about this.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Type Challenges: Implement the OmitByType<T, U> Utility Type
TypeScript Type challenges, TypeScript Omit Type, ... TypeScript 4.1 allows us to remapping keys in mapped types using the as clause.
Read more >Typescript: Cannot declare additional properties on Mapped ...
The {[K in XXX]: YYY} syntax is a mapped type, a special object type which iterates over the union members of keylike type...
Read more >Hibernate ORM 5.2.18.Final User Guide - Red Hat on GitHub
Hibernate not only takes care of the mapping from Java classes to database tables (and from Java data types to SQL data types),...
Read more >Gatsby Changelog | 5.3.0
ES Modules (ESM) in Gatsby files; Improved error messages. Also check out notable bugfixes. Bleeding Edge: Want to try new features as soon...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
it would be great if this was an actual feature. “dynamically” adding typings would be great in some scenarios
Please don’t take a production dependency on this; I have no idea what might be hiddenly broken when that type starts getting used.