You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I come from a C++ background where template programming / meta-programming is heavily used. The ISO C++ Standard defines type traits, which can be very useful in detection and modifying types in TypeScript.
Defining type traits in the TypeScript core language has a huge benefits in publicity and universal applicability instead of moving such a feature to an external extension. Type traits should extent the already known one: Partial, Readonly, Record and Pick.
Here are some type trait example, which can be usefull in TypeScript. Syntax, naming and wording is not fixed. Please have also a look at the standardized type traits in C++.
Please excuse my bad English. ;)
Structure
The type true represent a successful evaluation of a type trait.
The type false represents a unsuccessful evaluation of a type trait.
EnableIf
Enables a type T, if a is true. Otherwise the type is never.
Here is on real world example, where I (for LokiJS2, an in-memory database) would use it. The available filter operations depends on the type of the property:
In accordance with getting the type of any typeof expression I suggest to add common type traits to TypeScript.
I come from a C++ background where template programming / meta-programming is heavily used. The ISO C++ Standard defines type traits, which can be very useful in detection and modifying types in TypeScript.
Defining type traits in the TypeScript core language has a huge benefits in publicity and universal applicability instead of moving such a feature to an external extension. Type traits should extent the already known one: Partial, Readonly, Record and Pick.
Here are some type trait example, which can be usefull in TypeScript. Syntax, naming and wording is not fixed. Please have also a look at the standardized type traits in C++.
Please excuse my bad English. ;)
Structure
true
represent a successful evaluation of a type trait.false
represents a unsuccessful evaluation of a type trait.EnableIf
Enables a type T, if a is
true
. Otherwise the type isnever
.Possible implementation
Example
CompileIf
Compiles only if T is
true
. Otherwise a compile error will be raised.Possible implementation
Example
IsBoolean
The result is
true
, if the type isboolean
. Otherwisefalse
.Example
IsNumber
The result is true, if the type is
number
. Otherwisefalse
.Example
IsString
The result is
true
, if the type isstring
. Otherwisefalse
.Example
IsArray
The result is
true
, if the type isarray
. Otherwisefalse
.Example
IsObject
The result is
true
, if the type is object. Otherwisefalse
.Example
IsNull
The result is
true
, if the type isnull
. Otherwisefalse
.Example
IsClass
The result is
true
, if the type is a class type. Otherwisefalse
.Example
IsSame
The result is
true
, if two types are identical. Otherwisefalse
.Possible implementation
Example
IsExtension
The result is
true
, if a type is an extension of another type. Otherwisefalse
.Possible implementation
Example
RemoveExtend
Removes one extent from the given array type.
Possible implementation
Example
Real world example:
Here is on real world example, where I (for LokiJS2, an in-memory database) would use it. The available filter operations depends on the type of the property:
Final note
All examples above do already compile (except the real world example).
The text was updated successfully, but these errors were encountered: