Bug Report
π Search Terms
infer, Exclude, conditional typing
π Version & Regression Information
- This changed between versions 3.8.3 and 3.9.7
β― Playground Link
Playground link with relevant code
π» Code
type Simplify<T> = {[KeyType in keyof T]: T[KeyType]};
type optionalKeys<T extends object> = {
[k in keyof T]: undefined extends T[k] ? k : never;
}[keyof T];
type requiredKeys<T extends object> = Exclude<keyof T, optionalKeys<T>>;
export type addQuestionMarks<T extends object> = {
[k in optionalKeys<T>]?: T[k];
} & {
[k in requiredKeys<T>]: T[k];
};
type ZodRawShape = {
[k: string]: ZodType<any>;
};
interface ZodType<Output> {
_type: Output;
}
interface ZodObject<
T extends ZodRawShape,
Output = Simplify<
{
[k in optionalKeys<T>]?: T[k];
} & {
[k in requiredKeys<T>]: T[k];
}
>
> extends ZodType<Output> {
readonly _shape: T;
}
type MyObject<T> = T extends ZodObject<infer U>
? U extends ZodRawShape
? U
: never
: never;
π Actual behavior
infer U causes an error with requiredKeys using Exclude utility type:
Type 'U' does not satisfy the constraint 'ZodRawShape'.(2344)
The same inference works when requiredKeys is simplified to:
type requiredKeys<T extends object> = {
[k in keyof T]: undefined extends T[k] ? never : k;
}[keyof T];
The problem disappears when some type manipulations (Simplify, k in optionalKeys<T>, k in requiredKeys<T>) are removed.
π Expected behavior
Both requiredKeys implementations are expected to work similarly.
Bug Report
π Search Terms
infer, Exclude, conditional typing
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
infer Ucauses an error withrequiredKeysusingExcludeutility type:The same inference works when
requiredKeysis simplified to:The problem disappears when some type manipulations (
Simplify,k in optionalKeys<T>,k in requiredKeys<T>) are removed.π Expected behavior
Both
requiredKeysimplementations are expected to work similarly.