Primitive<T> is a TMP (Type Meta Programming) type which converts its
argument as a primitive type within the framework JSON.
If the target argument is a built-in class which returns its origin primitive
type through the valueOf() method like the String or Number, its return
type will be the string or number. Otherwise, if the built-in class does
not have the valueOf() method, the return type will be an empty object
({}).
Otherwise, if the target argument is a type of custom class, all of its
custom methods will be erased and its prototype will be changed to the
primitive object. Therefore, the return type of the TMP type will finally
be the primitive object.
In addition, if the target argument is a type of custom class and it has a
special method toJSON(), the return type of this Primitive will be not
Primitive<Instance> but Primitive<ReturnType<Instance.toJSON>>.
Before | After
------------------------|---------------------------------------- Boolean |
booleanNumber | numberString | stringClass | objectClass
with toJSON() | Primitive<ReturnType<Class.toJSON>> Native Class | never
Others | No change
Primitive type of JSON.
Primitive<T>
is a TMP (Type Meta Programming) type which converts its argument as a primitive type within the framework JSON.If the target argument is a built-in class which returns its origin primitive type through the
valueOf()
method like theString
orNumber
, its return type will be thestring
ornumber
. Otherwise, if the built-in class does not have thevalueOf()
method, the return type will be an empty object ({}
).Otherwise, if the target argument is a type of custom class, all of its custom methods will be erased and its prototype will be changed to the primitive
object
. Therefore, the return type of the TMP type will finally be the primitive object.In addition, if the target argument is a type of custom class and it has a special method
toJSON()
, the return type of thisPrimitive
will be notPrimitive<Instance>
butPrimitive<ReturnType<Instance.toJSON>>
.Before | After ------------------------|----------------------------------------
Boolean
|boolean
Number
|number
String
|string
Class
|object
Class
withtoJSON()
|Primitive<ReturnType<Class.toJSON>>
Native Class | never Others | No change