πͺStrongType
strongType
Create strong typed variables without TypeScript!
Examples
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";34
74389.2
StrongTypeError: "value doesn't match type in StrongType"const { defineStrongType } = require('coreplus')('strongType');
defineStrongType('strongString', 'Hello', String);
console.log(strongString);
strongString += " World";
console.log(strongString);
stringString = 92;Use
Class
First, require the StrongType class using the namespace strongType
Then assign a variable to a new StrongType
you can change / access the variable using .v
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
Then run the function to create a global typed variable
This variable is global, and can be accessed / changed anywhere with just the namespace
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