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

Feature request: strictPartial mode #21159

Closed
Jessidhia opened this issue Jan 12, 2018 · 4 comments
Closed

Feature request: strictPartial mode #21159

Jessidhia opened this issue Jan 12, 2018 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@Jessidhia
Copy link

This is a request to have a mode that has stricter optional object keys, such as the ones generated by the Partial mapped type. The idea is to allow reading a possibly-missing key, where its type will include |undefined, but not writing.

This is important for use cases that involve Object.assign or object spreads. Having a key assigned to undefined will overwrite an existing key by putting the value undefined, which currently allows you to break type correctness.

Code

interface Foo {
  bar: string
}

const foo1: Foo = { bar: '' } // ok
const foo2: Partial<Foo> = {} // ok
const foo3: Partial<Foo> = { bar: '' } // ok
const foo4: Partial<Foo> = { bar: undefined } // type error under --strictPartial

function test (foo: Foo, partial: Partial<Foo>) {
  const bar1 = foo.bar // typeof bar1 == string
  const bar2 = partial.bar // typeof bar2 == string|undefined
}

test(foo1, foo1)
test(foo1, foo2)
test(foo1, foo3)
// if some fancy type tracking is done on foo3, otherwise type error
test(foo3, foo3)

Example of current code that breaks because there is no strictPartial:

interface Foo {
  bar: string
}
const foo: Foo = { bar: '' }
const partial: Partial<Foo> = { bar: undefined } // would be an error under --strictPartial
const merged: Foo = { ...foo, ...partial } // accepted
const { bar } = merged // typeof bar is string
// bar is actually `undefined`!

The type definitions for React currently works around it by using generics, keyof and Pick, but this hack can only be used for function arguments, and the function must be converted into a generic function.

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/d0fdb378/types/react/index.d.ts#L283-L289

If a type actually means to accept undefined itself as a valid value, then its declaration should include |undefined. An additional standard mapped type (name 🚲🏠) can be used to not only make keys optional but also |undefined them, and those would be appropriate for use cases such as inputs to object destructuring with defaults.

@Jessidhia Jessidhia changed the title Feature request: strictPartial types Feature request: strictPartial mode Jan 12, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Jan 12, 2018

I am not sure i understand the proposal here. but i think the underlying issue is that the type system does not differentiate between a property set to undefined and one that is missing. this is tracked by #13195

@Jessidhia
Copy link
Author

Hm, it does seem related to that bug, but this is more about having a way to forbid writing undefined into an optional property. When the property is read, it should be accepted as possibly being undefined because it is marked as optional with ?:.

@mhegazy
Copy link
Contributor

mhegazy commented Jan 13, 2018

#13195 is exactelly what u are talking about. Making optional properties not accept undefined as a valid value.

@mhegazy mhegazy added the Duplicate An existing issue was already created label Jan 13, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants