πŸ’ͺStrongType

strongType

Create strong typed variables without TypeScript!

Examples

class.js
const { StrongType } = require('coreplus')('strongType');

const strongNumber = new StrongType(34, Number);

console.log(strongNumber.v);

strongNumber.v = 74389.2;
console.log(strongNumber.v);

strongNumber.v = "Hello World";
$ node class.js
34
74389.2
StrongTypeError: "value doesn't match type in StrongType"
global.js
const { defineStrongType } = require('coreplus')('strongType');

defineStrongType('strongString', 'Hello', String);

console.log(strongString);

strongString += " World";
console.log(strongString);

stringString = 92;
$ node global.js
"Hello"
"Hello World"
StrongTypeError: "value doesn't match type in StrongType"
// TypeScript
const strongNumber = new StrongType(3, Number); -> const strongNumber: Number = 3;
defineStrongType('strongString', 'Hello', String); -> const strongString: String = "Hello";

Use

Class

First, require the StrongType class using the namespace strongType

const { StrongType } = require('coreplus')('strongType');

Then assign a variable to a new StrongType

const foo = new StrongType(bar, Baz);

you can change / access the variable using .v

console.log(foo.v);
foo.v = 12;

StrongType( value?* , Type?*, allowUndefined?); - Argument: The value of the variable, not required if there is a Type.

StrongType(value?*, Type?* , allowUndefined?); - Argument: The type of the variable, defaults to the type of the value if not defined.

StrongType(value?*, Type?*, allowUndefined? ); - Argument: If the variable can be set to undefined, this argument is optional.

Global

First, require the defineStrongType function using the namespace strongType

const { defineStrongType } = require('coreplus')('strongType');

Then run the function to create a global typed variable

defineStrongType('foo', bar, Baz);

This variable is global, and can be accessed / changed anywhere with just the namespace

console.log(foo);
foo = 12;

defineStrongType( name , value?*, Type?*, allowUndefined?); - Argument: The name of the StrongType variable

defineStrongType(name, value?* , Type?*, allowUndefined?); - Argument: The value of the variable, not required if there is a Type.

defineStrongType(name, value?*, Type?* , allowUndefined?); - Argument: The type of the variable, defaults to the type of the value if not defined.

defineStrongType(name, value?*, Type?*, allowUndefined? ); - Argument: If the variable can be set to undefined, this argument is optional.

Last updated