-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Using keyof with key remapping to exclude index signatures doesn't return known keys #41966
Copy link
Copy link
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue
Milestone
Description
TypeScript Version: 4.2.0-dev.20201211
Search Terms:
key remapping
remap index signature
keyof returns number
Code
type Compute<A> = { [K in keyof A]: Compute<A[K]>; } & {};
type EqualsTest<T> = <A>() => A extends T ? 1 : 0;
type Equals<A1, A2> = EqualsTest<A2> extends EqualsTest<A1> ? 1 : 0;
type Filter<K, I> = Equals<K, I> extends 1 ? never : K;
type OmitIndex<T, I extends string | number> = {
[K in keyof T as Filter<K, I>]: T[K];
};
type IndexObject = { [key: string]: unknown; };
type FooBar = { foo: "hello"; bar: "world"; };
type WithIndex = Compute<FooBar & IndexObject>; // { [x: string]: {}; foo: "hello"; bar: "world"; } <-- OK
type WithoutIndex = OmitIndex<WithIndex, string>; // { foo: "hello"; bar: "world"; } <-- OK
type FooBarKey = keyof FooBar; // "foo" | "bar" <-- OK
type WithIndexKey = keyof WithIndex; // string | number <-- Expected: string
type WithoutIndexKey = keyof WithoutIndex; // number <-- Expected: "foo" | "bar"Expected behavior:
Using keyof with the above remapped type (WithoutIndex) should return the known keys (i.e. "foo" | "bar").
Actual behavior:
While the key remapping in OmitIndex appears to successfully omit the string index signature (e.g. OmitIndex<WithIndex, string> --> { foo: "hello"; bar: "world"; }); using keyof with the resulting type returns number.
Playground Link:
Related Issues:
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue