typeself provides self-referential types to JavaScript/TypeScript
JavaScript only has basic Data types and typeof operator returns a string indicating the type of the unevaluated operand.
TypeScript brings you optional static type-checking, however, it does not provide a type that has a totally unique property.
typeself ( https://github.com/kenokabe/typeself )
provides a unique self-referential type with the name of the function.
As the most simple example, a unique type: Type(Member)
is defined by an identity function: Member = a => a
const Member = a;
Here, the output of the function: Member
is a Proxy of a
.
Now, define alice
that has a string value as usual and bob
also has a string value, but in this case, has given type Member
.
const alice = "Alice";const bob = ;
The typed data is, in fact, a proxy of the original and behaves like the original. The proxy objects are rarely distinguishable than the original object. The proxy object is noticeable on, for instance, console.log
.
isType(Member)
returns Boolean(true/flase) for the data with Type(Member)
or non-typed.
;//Member status of Alice;//false;//Member status of Bob;//true
The original concept introduced in SICP as "TypeTag".
ESM
ES Modules are distributed as
./dist/build/modules/typeself.js
./dist/build/modules/primitive-obj.js
;
CJS
npm
Using npm:
$ npm i typeself
In Node.js:
const Type isType = ;
Test
$ node -r esm ./dist/build/index.js
./test/test-typeself.js
;; const test_typeself = { ; const Member = a; const alice = "Alice"; const bob = ; ;//Member status of Alice ;//false ;//Member status of Bob ;//true ; const specialOperation = f; const f1 = a + 1; //vanilla function const f2 = //typed function { //This function might be considered to be "special" //because it does some featured operations in a context. return a * 2; }; ;//false ;//2 // just in case, let you know ;//true ;//2 // just in case, let you know ; const I = a; //just a dummy function ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; const n = 6; ; }; ;