# 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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ephf.gitbook.io/coreplus/packages/strongtype.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
