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 pass optional property _a_ to the other type's optional property _b_ if --strictOptionalProperties=true #44390

Closed
tetsuharuohzeki opened this issue Jun 2, 2021 · 2 comments

Comments

@tetsuharuohzeki
Copy link
Contributor

Bug Report

🔎 Search Terms

🕗 Version & Regression Information

  • typescript@4.4.0-dev.20210602.
  • 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.

⏯ 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

interface A {
    a?: number;
}

function outer(input: A) {
    inner({
        // typescript compiler with `--strictOptionalProperties=true` will say here is error as
        //  > Type 'number | undefined' is not assignable to type 'number'.
        //  > Type 'undefined' is not assignable to type 'number'.ts(2322)
        a: input.a,
    });
}

function inner(input: A) {
}

🙁 Actual behavior

TypeScript compiler fail to compile the the above example with the reason I commented.
After I read #43947, I could not conclude the current behavior is expected design. So I filed this as a bug.

🙂 Expected behavior

I thinks this should be pass to compile.

Remarks

If we pass outer's input to inner directly like the following example, typescript compile success to compile.

interface A {
    a?: number;
    b: string;
}

interface B {
    a?: number;
}

function outer(input: A) {
    inner(input);
}

function inner(input: B) {
}
@fatcerberus
Copy link

strictOptionalProperties means, roughly, that an optional property must either exist with the specified type, or not exist at all. Allowing the assignment would mean you've possibly created an explicit property with the value undefined, which is counter to the purpose of the flag.

@tetsuharuohzeki
Copy link
Contributor Author

Ah, I see what #13195 aims. By that, this would not be a bug. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants