Typescript: describe primitive types

Created on 8 Jul 2019  Β·  3Comments  Β·  Source: microsoft/TypeScript

Search Terms

Suggestion

const n :Exclude<number, object> = Object(0);// error
const o :Exclude<boolean, object> = Object(false);// error
const s :Exclude<string, object> = Object('');// error

Use Cases

Currently, there is no way to limit api pass primitive value only.

Examples

Checklist

My suggestion meets these guidelines:

  • [ ] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [ ] This wouldn't change the runtime behavior of existing JavaScript code
  • [ ] This could be implemented without emitting different JS based on the types of the expressions
  • [ ] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • [ ] This feature would agree with the rest of TypeScript's Design Goals.
Unactionable

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

All 3 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kyasbal-1994 picture kyasbal-1994  Β·  3Comments

fwanicka picture fwanicka  Β·  3Comments

weswigham picture weswigham  Β·  3Comments

Zlatkovsky picture Zlatkovsky  Β·  3Comments

Antony-Jones picture Antony-Jones  Β·  3Comments