-
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
Partial<any> should be treated as any #20247
Comments
Do you have a real world use case for this? It appears to be intentional because type PartialString = Partial<string>; // string
type PartialNumber = Partial<number>; // number
type PartialObject = Partial<object>; // object
type PartialWeakObject = Partial<{}>; // {}
type PartialAny = Partial<any>; // { [x: string]: any; } |
Yes I do, as in the linked PR: This issue is breaking this case: |
By the way, type |
Now, I think there is something to be said on the use of Please see #19185 for more details about the change. Duplicate of #19624 |
I think #19185 may be over-solving the issue in #19152. I can understand that But that doesn't mean In #19152, the problem is the indexer, not the property type, that have the problem. Also, I understand that |
a use-case equivalent to this: function consumer(p:{foo:string}){}
const obj :Partial<any> = {foo:'bar'};
consumer(obj); // TS2345:Argument of type 'Partial<any>' is not assignable to parameter of type '{ foo: string; }'. Property 'foo' is missing in type 'Partial<any>'. currently blocks wix-incubator/wix-react-tools#188 |
@amir-arad any change here wouldn't solve the issue with your code. It is unrelated. For example the following is an error: function consumer(p:{foo:string}){}
const obj: Partial<{ foo: string }> = {foo:'bar'};
consumer(obj); You have a situation where the key you require is optional on the source type. That won't ever work. |
from the Documentation site :
I expect function consumer(p:{foo:string}){}
const obj: (any & {foo:null}) | {foo:number} = {foo:'bar'};
consumer(obj); // this works fine |
To nail this a bit more, I can see the rationale for #19185 type X<T, R> = {
[P in keyof T]?: R;
};
let x: X<{ a: number, b: string }, { foo: number }> = { a: { foo: 1 }, b: { foo: 2 } }
// y: { [P: string]: { foo: number } }
let y: X<any, { foo: number }> = { c: { foo: number } }
// z: { a: any, b: any }
let z: X<{ a: number, b: string }, any> = { a: 'a', b: 1 } But |
all mapped types have the same behavior for primitives.. |
Then why not use |
fair enough. As I said, I agree that the current behavior for mapped type is "likely" to be fine. type X<T> = {
[P in keyof T]: T[P]
}
X<any> => { [P in keyof any]: any[P] } => { [P: string]: any } My question is: should "a partial set of an infinite set is an infinite set" |
Mapped types in general are defined in terms of object types. in that context |
Yes, on that argument it does make sense for going against I'm almost convinced to drop But on the other hand 😛 : const x: Readonly<number> = 1
const y: Readonly<number & any> = 1 // error
Maybe the special treatment needed for primitives is the root cause of this confusing behavior. Maybe Need more time to think about EDIT: while let x: Readonly<{ foo: number }> => x: { readonly foo: number }
// !=
x: readonly { foo: number }
// or
x: readonly { readonly foo: number } But let x: Readonly<number> => x: number?
// or even
x: readonly number that special treatment for primitive does create some problems. It feels like an escape clause then some actually desirable behaviors. |
we wanted to do that originally. but then 1. |
@kitsonk It's not always possible or convenient to define the type directly. sometimes you need to work with a third party tool, that returns |
declare function foo<T>(bar: T): Partial<T>;
const baz: any = { qat: 1 };
const qat = foo(baz) as any;
// or
const qux: any = foo(baz); It just requires you to be explicit about your |
@kitsonk const baz: any = { qat: 1 };
const qat = func1(func2(foo(baz) as any) as any) as any; // this is not good code |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
|
gcanti/typelevel-ts#19 suggested using conditional types to accomplish the desired behavior. |
[email protected]
Partial<any>
should be considered asany
and can be assigned to other types.Playground
The text was updated successfully, but these errors were encountered: