Assuming the given type:
export declare type MongoManagerFilter<T> = {
[P in keyof T]?: (P extends keyof T ? T[P] : any);
};
When trying to specify the filter:
update.$setOnInsert.createdAt = new Date();
I get this error:
Type 'Date' is not assignable to type '"createdAt" extends keyof T ? T[keyof T & "createdAt"] : any'.ts(2322)
But if I change the filter to this it works:
export declare type MongoManagerFilter<T> = {
[P in keyof T]?: any;
};
Why does the conditional statement not actually figure out the type? It's just keeping the code as the type.
I know the filter doesn't make sense how it is, I have another type instead of keyof T
to catch dot notation, but I am having the same issue with conditional statements not figuring out the actual type.
Full example
interface CollectionDocument {
_id?: string;
createdAt: Date;
}
type PathImpl<T, Key extends keyof T> =
Key extends string
? T[Key] extends Record<string, any>
? `${Key}.${PathImpl<T[Key], Exclude<keyof T[Key], keyof Date | keyof Object | keyof string> & string>}`
| `${Key}.${Exclude<keyof T[Key], keyof Date | keyof Object | keyof string> & string}`
: never
: never;
type Path<T> = keyof T | PathImpl<T, keyof T>;
type PathValue<T, P extends Path<T>> =
P extends `${infer Key}.${infer Rest}`
? Key extends keyof T
? Rest extends Path<T[Key]>
? PathValue<T[Key], Rest>
: never
: never
: P extends keyof T
? T[P]
: any;
export declare type MongoManagerFilter<T> = {
[P in Path<T>]?: PathValue<T, P>;
};
export class MongoManagerCollection<T extends CollectionDocument> {
constructor() {}
updateOne(filter: MongoManagerFilter<T>) {
// THIS DOES NOT WORK
filter._id = '';
filter.createdAt = new Date();
}
}
// THIS WORKS
let testModel = new MongoManagerCollection<CollectionDocument>();
testModel.updateOne({_id: 'test', createdAt: new Date()});
EDIT Adding another layer to this playground for more issues along same lines:
Seems I am getting this error as well due to the recursion in the types:
Type instantiation is excessively deep and possibly infinite.ts(2589)