Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot narrow the optional properties to T properly through (T | undefined) => T if --strictOptionalProperties=true #44388

Closed
tetsuharuohzeki opened this issue Jun 2, 2021 · 0 comments · Fixed by #44565
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@tetsuharuohzeki
Copy link
Contributor

tetsuharuohzeki commented Jun 2, 2021

Bug Report

🔎 Search Terms

🕗 Version & Regression Information

  • This changed between versions typescript@4.4.0-dev.20210601 and typescript@4.4.0-dev.20210602
    • Of course, this will not be happen if --strictOptionalProperties=false. But I could not think this is reasonable behavior.

⏯ Playground Link

Playground link with relevant code

At this moment which I filed this issue, typescript playground does not prepare to use 4.4.0-dev.20210602.

💻 Code

type Undefinable<T> = T | undefined;

function expectNotUndefined<T>(value: Undefinable<T>): T {
    if (value === undefined) {
        throw new TypeError('value is undefined');
    }

    return value;
}

interface Bar {
    bar?: number;
}

function a(input: Bar): void {
    // This variable should be expected as `number`
    // but typescript compiler with `--strictOptionalProperties=true` infer this variable as `number | undefined`.
    const notUndefinedVal = expectNotUndefined(input.bar);

    // TypeScript compiler says:
    //  > Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
    //  > Type 'undefined' is not assignable to type 'number'.ts(2345)
    b(notUndefinedVal);
}

function b(input: number) {
    // something
}

🙁 Actual behavior

The above code will be compile error as I commented.

🙂 Expected behavior

  • Pass to compile.
    • typescript compiler should infer notUndefinedVal as number.
  • I think this would be a bug of strictOptionalProperties and this behavior is not reasonable.

...or are there any workaround?

@tetsuharuohzeki tetsuharuohzeki changed the title Cannot narrow the optional properties to T properly with --strictOptionalProperties if the function takes T | undefined and return T Cannot narrow the optional properties to T properly with --strictOptionalProperties=true Jun 2, 2021
@tetsuharuohzeki tetsuharuohzeki changed the title Cannot narrow the optional properties to T properly with --strictOptionalProperties=true Cannot narrow the optional properties to T properly through (T | undefined) => T if --strictOptionalProperties=true Jun 2, 2021
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jun 10, 2021
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.4.0 (Beta) milestone Jun 10, 2021
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Jun 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants