Closed
Description
Bug Report
π Search Terms
const type, validator type
π Version & Regression Information
5.0.2
- I was unable to test this on prior versions because const type parameters are a new feature
β― Playground Link
Playground link with relevant code
π» Code
type NotEmpty<T extends Record<string, any>> = keyof T extends never ? never : T;
const thing = <const O extends Record<string, any>>(o: NotEmpty<O>) => {
return o;
};
const t = thing({foo: ''});
// is inferred as { foo: string } β
// BUT, when adding a mapped type to NotEmpty it works
type NotEmptyMapped<T extends Record<string, any>> = keyof T extends never ? never : {
[K in keyof T]: T[K] // β¬
οΈ
};
const thingMapped = <const O extends Record<string, any>>(o: NotEmptyMapped<O>) => {
return o;
};
const tMapped = thingMapped({foo: ''});
// is inferred as { foo: "" } β
π Actual behavior
When using a non-mapped validator type, the "valid" branch does not preserve the const-like inference unless the type is mapped over.
π Expected behavior
A mapped type is not needed to preserve const-like inference.