Skip to content

Typescript not understand Promise than catch logic #25022

Closed
@vforv

Description

@vforv
    "typescript": "^2.7.2",
    "typescript-eslint-parser": "^14.0.0",

If I have promise like this:

export default function to<T>(promise: Promise<T>) {
    return promise
        .then((data: T) => {
            return { error: undefined, data };
        })
        .catch((error: Error) => {
            return { error, data: undefined };
        });
}

And after that I add async await with this logic:

public someMethod() {
const { error, data } = await to<SomeType>(SOME PROMISE HERE);

if(error) {
  return;
}

// HERE I WILL NOT BE ABLE TO GET SomeType because Typescript still recognise data as type:
// undefined | SomeType
data.
}

Maybe this is some bug with IDE I don't know. I am usigin Visual Code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions