const n :Exclude<number, object> = Object(0);// error
const o :Exclude<boolean, object> = Object(false);// error
const s :Exclude<string, object> = Object('');// error
Currently, there is no way to limit api pass primitive value only.
My suggestion meets these guidelines:
That all works already:
declare function f(n: number): void;
// Argument of type 'Number' is not assignable to parameter of type 'number'.
// 'number' is a primitive, but 'Number' is a wrapper object. Prefer using 'number' when possible.
f(new Number(5));
The problem you're hitting is this overload declaration:
interface ObjectConstructor {
(value: any): any;
}
More specific overloads could be added to ObjectConstructor. My guess is there's not much demand for it? It's certainly an odd thing to see in code.
@nmain Thank you!
new Number is warned by my IDE, and some types has no new constructing syntax like Object(Symbol()) or Object(1n), so I use Object which also shorter. That makes me didn't notice that ObjectConstructor made output any.
Then this feature request should be changed as you said, just more specific overload add to ObjectConstructor.
Before that, using new Number instead and close my IDE notice is also ok, but currently, I will try to override type declaration of Object by myself (Exclude<Number, number> seems not work, so I use Number & object instead):
declare const Object :{
<T extends object> (value :T) :T;
(value? :undefined | null) :object;
(value :boolean) :Boolean & object;
(value :number) :Number & object;
(value :string) :String & object;
(value :symbol) :Symbol & object;
(value :bigint) :BigInt & object;
new<T extends object> (value :T) :T;
new (value? :undefined | null) :object;
new (value :boolean) :Boolean & object;
new (value :number) :Number & object;
new (value :string) :String & object;
new (value :symbol) :Symbol & object;
new (value :bigint) :BigInt & object;
};
If you want a function that only takes primitives, you can just list them (there aren't many). If that's not what you're looking for, the suggestion is not phrased clearly enough to be actionable
Most helpful comment
If you want a function that only takes primitives, you can just list them (there aren't many). If that's not what you're looking for, the suggestion is not phrased clearly enough to be actionable