You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using some helper functions to construct type guards where the type predicate is inferred:
constisObject=(value: unknown): value is {[key: string]: unknown}=>typeofvalue==='object'&&value!==nullconstisObjectWith=<S>(predicates: {readonly[PinkeyofRequired<S>]: (value: unknown)=>value is S[P]},)=>(value: unknown): value is S=>isObject(value)&&entries(predicates).every(([k,p])=>p(value[k]))constisNumber=(x: unknown): x is number=>typeofx==='number'constentries=<Vextendsobject>(object: V): ReadonlyArray<readonly[string&keyofV,V[string&keyofV]]>=>Object.entries(object)asArray<[string&keyofV,V[string&keyofV]]>exportconstisFoo=isObjectWith({a: isNumber,})exportconstisBar=isObjectWith({b: isFoo,})
Expected behavior:
The .d.ts file should contain all the inferred property types:
export declare const isFoo: (value: unknown) => value is {
a: number;
};
export declare const isBar: (value: unknown) => value is {
b: {
a: number;
};
};
//# sourceMappingURL=repro.d.ts.map
Actual behavior:
The .d.ts file contains any for the nested property type:
export declare const isFoo: (value: unknown) => value is {
a: number;
};
export declare const isBar: (value: unknown) => value is {
b: {
a: any;
};
};
//# sourceMappingURL=repro.d.ts.map
Interestingly, if I force TypeScript to "evaluate" the inferred type using a mapped type, I get the correct result:
const _isFoo = isObjectWith({
a: isNumber,
})
type GuardedType<T> = T extends (value: any) =>
value is infer S ? { [K in keyof S]: S[K] }
: never
export const isFoo: (value: unknown) => value is GuardedType<typeof _isFoo> = _isFoo
TypeScript Version: 3.5.3
Search Terms: declaration any inferred type
Code
I'm using some helper functions to construct type guards where the type predicate is inferred:
Expected behavior:
The
.d.ts
file should contain all the inferred property types:Actual behavior:
The
.d.ts
file containsany
for the nested property type:Interestingly, if I force TypeScript to "evaluate" the inferred type using a mapped type, I get the correct result:
Related Issues: #19565
The text was updated successfully, but these errors were encountered: