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

Usages of Partial accept primitives #14224

Closed
glazar opened this issue Feb 22, 2017 · 5 comments
Closed

Usages of Partial accept primitives #14224

glazar opened this issue Feb 22, 2017 · 5 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@glazar
Copy link

glazar commented Feb 22, 2017

TypeScript Version: 2.2

Code

interface ComponentState {
  a: string;
  b: number;
  c: boolean;
}

const setState = (state: Partial<ComponentState>) => {
   ....
}

setState(1)
setState("1")
setState(true)

Expected behavior:

Primitives should not be allowed to be passed in arguments.
Primitives should be excluded when using Partial.

The new object type, added with 2.2, can be used implicitly when using Partial construct without having to explicitly use it to have a workaround for this issue.

Actual behavior:

Usages of Partial accept primitives, even though the intent is that primitives be excluded.
Can explicitly also use the new object type, added with 2.2, to have a workaround for this issue.

@mhegazy
Copy link
Contributor

mhegazy commented Feb 22, 2017

@mhegazy mhegazy added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Feb 22, 2017
@glazar
Copy link
Author

glazar commented Feb 27, 2017

@mhegazy
I understand why this happens - https://github.com/Microsoft/TypeScript/wiki/FAQ#why-are-all-types-assignable-to-empty-interfaces - when you manually create an interface which no members or with all optional members.

I strongly believe that the intent of Partial is to provide support for creating interfaces which all optional members while also enforcing that usages of it do not accept primitives.

The new object type can be used to cover the entire intent as mentioned in #14249.

@mhegazy
Copy link
Contributor

mhegazy commented Feb 27, 2017

#14249 talks about adding a constraint on the parameter of Partial, it says nothing about how the type is compared.

Mapped types are just a transformation, the generated type is just like any type you would write manually.. e.g. Patial<{ a: number, b:string}> === { a?: number, b?: string }.

@glazar
Copy link
Author

glazar commented Feb 28, 2017

@mhegazy
I just realized I made a mistake by referencing #14249. I now realise that the intent for that one was different, I just mistakenly read the code example. I was actually reading Partial<T> extends object instead of Partial<T extends object>.

I am just fine with using Partial as simply a mapped typed, just as a transformation, and then manually add the intent of always being an object. It is not a workaround but an explicit way of doing what I initially thought would be done implicitly.

Thanks for all the clarifications. They have helped me while having this issue discussed.

@mhegazy
Copy link
Contributor

mhegazy commented Feb 28, 2017

Issue #7485 better tracks the request. this is not unique to mapped types as i mentioned earlier. with an appropriate fix for #7485, your scenario should be flagged as an error too.

@mhegazy mhegazy closed this as completed Apr 21, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

2 participants