'Typescript issue: Type '...' is not assignable to type '...'
I'm using nestJS,
I have 2 services,
in the 1st service:
async getOrganizations (attributes: (keyof Organization)[]): Promise<Partial<Organization>[]> {
const organizations = await this.organizationRepository.find({select: attributes})
return organizations;
//returns [{id:1, name: "x"},{id:2, name: "y"},..]
}
in the 2nd service:
async getOrganizations(): Promise<CustomerResponseDto[]> {
const attributes : (keyof Organization)[] = ['id', 'name'];
let organizations = await this.1stService.getOrganizations(attributes);
organizations = organizations.map((organization)=>{
return {
...organization,
type: 'Organization'
}
})
return organizations;
//returns [{id:1, name: "x", type: 'Organization'},{id:2, name: "y", type: 'Organization'},..]
}
and the CustomerResponseDto:
export interface CustomerResponseDto {
readonly id: number;
readonly name: string;
readonly type: string;
}
now I'm getting the following error:
Type 'Partial<Organization>[]' is not assignable to type 'CustomerResponseDto[]'.
Property 'type' is missing in type 'Partial<Organization>' but required in type 'CustomerResponseDto'.
22 | })
23 |
> 24 | return organizations;
| ^^^^^^^^^^^^^^^^^^^^^
25 | }
please advise
Solution 1:[1]
It works now, the issue was related to variable naming
in 2nd service
I changed:
organizations = organizations.map((organization)=>{
return {
...organization,
type: 'Organization'
}
to be
const organizationsWithType = organizations.map(({ id, name })=>{
return {
id,
name,
type: 'Organization'
}
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mohammed Amin |
