I'm encountering an issue related to destructuring and types. In my scenario, I have defined a custom type and a function that returns that specific type:
interface CustomNameInfo {
name: string;
access: string
}
async getNameInfo(): Promise<CustomNameInfo> {
//logic
return {
name: "FOO",
access: "none"
}
}
When using approach 1, everything works fine without any type errors:
let name, access, resp;
if (!hasName) {
resp = await this.getNameInfo();
access = resp.access;
name = resp.name;
}
However, when attempting to use the approach involving destructuring, TypeScript throws the error: Property 'access' and
namedoes not exist on type
CustomNameInfo``
let name, access;
if (!hasName) {
({ name, access } = await this.getNameInfo());
}