TypeScript Version: 5.5.4
Code:
type Trans<T> = Omit<T, never>; // A generic type that should return T without modification.
interface T1 {
a: string;
}
interface T2 {
a: string;
b: number;
}
type X1 = Trans<keyof T2>; // Expected: 'a' | 'b';
type X2 = T1 & T2;
type X3 = Trans<T1 & T2>; // As expected: T1 & T2.
Issue:
Unexpected:
type X1 = {
readonly [x: number]: string;
[Symbol.iterator]: () => StringIterator<string>;
toString: () => string;
charAt: (pos: number) => string;
charCodeAt: (index: number) => number;
concat: (...strings: string[]) => string;
... 43 more ...;
at: (index: number) => string | undefined;
}
My Question:
Why does X1
become this complex string type instead of the expected 'a' | 'b'
?
This behavior appears when combining keyof
with Omit
in a generic context, and I’m trying to understand the reasoning behind this type inference. Any insights or explanations would be greatly appreciated.
Search Terms:
keyof
Omit
generic type inference
TypeScript
I’m not reporting this as a bug but rather seeking clarification on the behavior I’m observing.
In the above code:
Trans<T>
is a generic type that doesn’t modifyT
, as it simply omitsnever
.- I expect
X1
to be'a' | 'b'
, askeyof T2
should infer the keys ofT2
.
However, X1
is inferred as a complex string-related type, including methods like charAt
, concat
, etc., which seems unexpected.