英文:
Type '{ id: string; }' is not assignable to type 'DeepPartial<T>'
问题
This error is occurring because TypeScript is not able to infer that the where
property in your SearchOneOptions<T>
interface should match the type DeepPartial<T>
. To fix this error, you can explicitly specify the type parameter when calling the b
function like this:
const b = <T extends { id: string }>(t: T) => {
const a: SearchOneOptions<T> = {
where: {
id: t.id
}
}
}
This will ensure that TypeScript correctly infers the type of where
as DeepPartial<T>
based on the type of T
that you provide when calling the b
function.
Please note that I've just provided the translation of the code part, as you requested.
英文:
I have the following code trying to create a generic function to abstract my repository infra to create a where clausule.
export type DeepPartial<T> = T extends object
? {
[P in keyof T]?: DeepPartial<T[P]>;
}
: T;
export interface SearchOneOptions<ENTITY extends { id: string }> {
/** Names of object attributes nested in the result object that must be filled, if possible */
includeNested?: string[];
/** Entities that can be soft deleted won't appear in queries by default */
withDeleted?: boolean;
select?: string[];
where?: DeepPartial<ENTITY>
}
const b = <T extends { id: string }>(t: T) => {
const a: SearchOneOptions<T> = {
where: {
id: t.id
}
}
}
And I'm facing the following error
Type '{ id: string; }' is not assignable to type 'DeepPartial<T>'.
Here is a playground to reproduce
Do you know how to solve this error? Thank you very much
答案1
得分: 1
在`DeepPartial`中的条件类型不是必要的,因为在原始类型上的映射类型会导致原始类型值。
```ts
export type DeepPartial<T> = {
[P in keyof T]?: DeepPartial<T[P]>;
};
<details>
<summary>英文:</summary>
The conditional type in `DeepPartial` is not necessary since a mapped type on a primitive results in the original primitive value.
```ts
export type DeepPartial<T> = {
[P in keyof T]?: DeepPartial<T[P]>;
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论