> For the complete documentation index, see [llms.txt](https://ephf.gitbook.io/coreplus/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ephf.gitbook.io/coreplus/packages/strongtype.md).

# StrongType

strongType

Create strong typed variables without TypeScript!

## Examples

{% code title="class.js" %}

```javascript
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";
```

{% endcode %}

{% code title="$ node class.js" %}

```shell
34
74389.2
StrongTypeError: "value doesn't match type in StrongType"
```

{% endcode %}

{% code title="global.js" %}

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

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

console.log(strongString);

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

stringString = 92;
```

{% endcode %}

{% code title="$ node global.js" %}

```bash
"Hello"
"Hello World"
StrongTypeError: "value doesn't match type in StrongType"
```

{% endcode %}

```typescript
// 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`

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

Then assign a variable to a new `StrongType`

```javascript
const foo = new StrongType(bar, Baz);
```

you can change / access the variable using `.v`

```javascript
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`

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

Then run the function to create a global typed variable

```javascript
defineStrongType('foo', bar, Baz);
```

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

```javascript
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.
