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

Optional object property that must be a spesific type and can't be undefined #56803

Closed
babakfp opened this issue Dec 16, 2023 · 3 comments
Closed
Labels
Question An issue which isn't directly actionable in code

Comments

@babakfp
Copy link

babakfp commented Dec 16, 2023

πŸ”Ž Search Terms

.

πŸ•— Version & Regression Information

.

⏯ Playground Link

https://www.typescriptlang.org/play?#code/C4TwDgpgBAsiAq5oF4BQUNQD5QN5QDsBDAWwgC4oBnYAJwEsCBzKAX3Ux32LMpoeYBuKAHswweiOIAbSnQCu0dqgDGUmlBIgAakWmKoyPBww8KUAEQWANCdHjJMyvIIATCADNGEV9agB6fygAdQB5ACUAaQBlPwAhAFV4KABJKABRAA0ABXSAYWSU5PhQjPDwiNR2IA

πŸ’» Code

type MyType =
    | { name: string }
    | { name: string; optional: true }

πŸ™ Actual behavior

const myValue = {
    name: "",
    optional: undefined, // WORKS
}

πŸ™‚ Expected behavior

const myValue = {
    name: "",
    optional: undefined, // ERROR: Type 'undefined' is not assignable to type 'true'.
}

Additional information about the issue

I don't know if this should be a bug report or a feature request.

@fatcerberus
Copy link

Not a bug. { name: "", optional: undefined } is assignable to { name: string } due to structural subtyping. You're looking for #12936.

@fatcerberus
Copy link

Optional object property that must be a [specific] type and can't be undefined

The above having been said: if this is your only use case for the requested behavior, then you can get that simply by enabling exactOptionalPropertyTypes.
https://www.typescriptlang.org/tsconfig#exactOptionalPropertyTypes

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Dec 19, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Question" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants