question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TS 4.1+ Key remapping in mapped types allows bypassing statically known members check

See original GitHub issue

Bug 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:open
  • Created 3 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
crowlKatscommented, Mar 15, 2021

it would be great if this was an actual feature. “dynamically” adding typings would be great in some scenarios

2reactions
RyanCavanaughcommented, Mar 11, 2021

Please don’t take a production dependency on this; I have no idea what might be hiddenly broken when that type starts getting used.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Mapped Types - TypeScript
Generating types by re-using an existing type.
Read more >
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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found