-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Don't allow explicit undefined fields on unions that don't have an undefined case #56398
Comments
Duplicate of #20863. But even with that you can't really rely on it, because TypeScript is structurally typed, excess properties are generally allowed. You'd need "exact types" to avoid this issue. #12936 This is perfectly valid code: const obj = { id: 123, title: '', description: '' }
const post: INewForumPost = obj And wow, you use excessive amounts of words as search terms. I think the idea of @fatcerberus to add a "summary" field is really good (mentioned in another issue). |
FWIW, |
Oh sorry I totally misread the template for the search terms part, I'll fix that now |
Okay hopefully that's better Would it be more reasonable to propose a strict setting that would require adding an undefined or typeof check to these kinds of unions instead of just the |
You can do this today: interface INewForumPost extends Omit<IForumPost, 'id'> {
id?: never;
} |
Oh awesome yeah that does what I need. Looks like I actually want to leave the |
π Search Terms
union, undefined, keys, non-shared, properties
β Viability Checklist
β Suggestion
I think unions should enforce that keys can't be assign
undefined
if neither half of the union accepts it. If I can't assign an object to either interface, I wouldn't expect to be able to assign it to the union of the 2.As an example, let's say I have these 2 interfaces:
I don't have a scenario where the
id
field should be allowed to exist, but beundefined
. Both of these object declarations will throw an error:The problem I had is that
id
can be undefined if I have a union of these 2:The part that had this lead to a bug in my code is that TS then doesn't let me simply do an
if (post.id)
orif (typeof post.id === 'string')
check to determine which side of the union I have (those checks would be safe here), but it makes me doif ('id' in post)
check (which is not safe here because it's true forundefined
).π Motivating Example
Let's say I have a function that tries to route the changes correctly to either "update" or "create":
And I am creating this
post
object using a form that works for both editing or creating:That
id
assignment is a pretty innocent ternary, but actually should not be accepted.saveForumPost
now treats this as an existing entry and attempts to update instead of create. This change would require me to either provide a proper value forid
or omit the key completely, leading to safer code.π» Use Cases
1. What do you want to use this for?
I would like to use this to ensure stronger type safety in my code.
2. What shortcomings exist with current approaches?
Current approaches allow assigning invalid objects to unions.
3. What workarounds are you using in the meantime?
I have fixed the code, as needed, but not until after my bug was found. The workaround for now is just to be extra careful in this type of situation.
The text was updated successfully, but these errors were encountered: