Why is there no error or warning displayed in the subscribe parameter when defining the wrong parameter type in tsconfig?
For example, in the correct code below, if 'user' is of type 'UserDto', VS Code will identify it correctly. However, if a different type like 'AddressDto' is used, neither VS Code nor Typescript will flag it as an error.
This is due to common properties among all Dtos. For instance, if all Dtos have an ID property, the error check becomes ineffective. Is there a way to receive a hint that the exact type for the user variable is not being used?
Is there a tsconfig option or solution for this issue?
export interface UserDto {
id?: string;
givenName?: string | null;
surname?: string | null;
}
public onAcceptInvitation(): void {
this.usersService.getSelf()
.subscribe((user: UserDto): void => {
Object.entries(user)
});
}
The 'AddressDto' has the property 'id'. Therefore 'AddressDto' and 'UserDto' have at least one property in common and TS will accept this.
export interface AddressDto {
id?: string;
street?: string | null;
houseNumber?: string | null;
}
public onAcceptInvitation(): void {
this.usersService.getSelf()
.subscribe((user: AddressDto): void => {
Object.entries(user)
});
}
The Api Endpoint looks like this:
public getSelf(observe?: 'body', reportProgress?: boolean, options?: {httpHeaderAccept?: 'text/plain' | 'application/json' | 'text/json'}): Observable<UserDto> {
return this.httpClient.get<UserDto>(`${this.configuration.basePath}/api/Users/self`,
{
responseType: <any>responseType,
withCredentials: this.configuration.withCredentials,
headers: headers,
observe: observe,
reportProgress: reportProgress
}
);
}
Solution: The issue lies in the Interface definition. When properties are nullable, Typescript may accept the new type without displaying errors.