Typings
ActiveJS is strictly typed, it does its best to infer and enforce as many typings as possible. However, where it can't infer a type, it uses generics so that you can pass a type.
For example, Units, Systems, and Action accept a type for their value. Cluster doesn't need to be passed types, as it can infer the types from the provided members automatically.

Units

​BoolUnit, NumUnit, and StringUnit don't accept a type because it's already implied as boolean, number, and string, respectively.
​ListUnit's value already has an array type, but it can't know about the type of items that the array is going to contain. You can pass it at the time of instantiation.
1
// a ListUnit whose value type would be string[]
2
const namesList = new ListUnit<string>();
3
​
4
namesList.push('Mr. Anderson'); // works
5
namesList.dispatch(['Trinity', 'Morpheus']) // works
6
​
7
namesList.push(737); // linting error, invalid value
8
namesList.dispatch([737]) // linting error, invalid value
Copied!
​DictUnit's value already has an object literal type, but it doesn't know the type of key-value pairs it's going to hold. You can pass it at the time of instantiation.
1
// a DictUnit whose value type would be {[key in 'a' | 'b' | 'c']: number}
2
const someDict = new DictUnit<{[key in 'a' | 'b' | 'c']: number}>();
3
​
4
someDict.set('a', 1993) // works
5
someDict.dispatch({a: 1, b: 2, c: 3}) // works
6
​
7
someDict.set('b', 'not a number') // linting error, invalid value
8
someDict.set('d', 1993) // linting error, invalid key
9
someDict.dispatch({a: 1, b: 2}) // linting error, property 'c' is missing
Copied!
​GenericUnit's value has an any type, it allows all types of values and leaves the typings to the developer. You can pass it at the time of instantiation.
1
// a GenericUnit whose value type would be string | number | boolean
2
const primitivesUnit = new GenericUnit<string | number | boolean>();
3
​
4
primitivesUnit.dispatch(42); // works
5
primitivesUnit.dispatch('HanuMan'); // works
6
primitivesUnit.dispatch(true); // works
7
​
8
primitivesUnit.dispatch(['you and I']) // linting error, invalid value
9
primitivesUnit.dispatch({youAreMine: true}) // linting error, invalid value
Copied!

Systems

​AsyncSystem doesn't have a value of its own, it derives value from its member Units, 3 of them are GenericUnits and 1 is BoolUnit. The three GenericUnits can be supplied types through AsyncSystem's generics.
1
type UserQuery = number; // type for queryUnit's value
2
type UserData = string; // type for dataUnit's value
3
type UserError = {status: number, message: string}; // type for errorUnit's value
4
​
5
// pass the types to AsyncSystem
6
const userSystem = new AsyncSystem<UserQuery, UserData, UserError>();
7
​
8
userSystem.queryUnit.dispatch(666) // works
9
userSystem.dataUnit.dispatch('evil string') // works
10
userSystem.errorUnit.dispatch({status: 418, message: "I'm a teapot"}) // works
11
​
12
userSystem.queryUnit.dispatch('not a number') // linting error, invalid value
13
userSystem.dataUnit.dispatch(420) // linting error, invalid value
14
userSystem.errorUnit.dispatch("I'm a teapot") // linting error, invalid value
Copied!
​Custom AsyncSystem doesn't need to be passed types explicitly, since it doesn't create Units itself, they are provided externally, and it can infer the types from the provided Units.

Action

​Action has any as its default value type, it can be restricted by passing a specific type through generics.
1
// an Action that will allow strings
2
const heroAction = new Action<string>();
3
​
4
heroAction.dispatch('fight injustice') // works
5
heroAction.dispatch('defeat bullies') // works
6
​
7
heroAction.dispatch(['catch', 22]) // liniting error, invalid value
Copied!
​
Last modified 1yr ago
Copy link