The Elm Architecture for typescript
This is an implementation of The Elm Architecture (TEA) for typescript.
Note: TEA has managed effects, meaning that things like HTTP requests or writing to disk are all treated as data in TEA. When this data is given to an Effect Manager, it can do some "query optimization" before actually performing the effect. Your application should consist of pure functions only and all effects should be handled in Effect Managers outside your application.
TEA has two kinds of managed effects: commands and subscriptions.
How to use
yarn add @typescript-tea/core
Please see the documentation site.
This is the usual counter app example using the react as view library. It is also available in this repo.
;;;;// -- STATE;;// -- UPDATE;// -- VIEW;// -- PROGRAM;// -- RUN;;Program.runprogram, render;
Differences from TEA in Elm
There are some naming differences from TEA in Elm:
Msgwas renamed to
Modelwas renamed to
Elm is a pure language with strict guarantees and the Effect Managers are part of kernel in Elm and you cannot (for good reasons) write your own Effect Managers in Elm. Typescript is an impure lanauge without any guarantees so it (probably) does not make sense to have this restriction. Therefore in typescript-tea it is possible to write your own Effect Manager to do whatever you want.
It does not have a built-in view library, instead it is possible to integrate with existing view libraries like React.
How to import
Whole module from the root
This package (and others in
@typescript-tea organization) exports only
types grouped into modules. You can import a module from the root of the package in the following way:
Unprefixed named imports from the module file
If you don't want to prefix with
ModuleName you can also use named imports directly from the module file:
Modules that export a single type
A common pattern is to have a module that exports a single type with the same name as the module. For example the
Result module does this, it exports the
Result type, some constructor functions that create a
Result type, and some utility funcitons that operate on or return a
Result type. In these cases it can become annoying to prefix the type with the module name, like
Result.Result. Consider the following example. Note that this is not how it is done for modules with single type exports in typescript-tea, it is just to illustrate how it would be done normally:
To avoid having to write
Result.Result in these cases, the
Result module uses a trick so that both the module name and the type can be named simply
Result. So the code above will become this (notice use of
Result for the type annotations instead of
How can this work? Well, the index file in the package does this to make it work:
I think it is somehow related to declaration merging in typescript :-).
Please note that this only work for modules that export a single type. If two types are exported it is not possible to use this shortcut because the exported
const will not contain any types.
How to develop
Node version >=12.6.0 is needed for development.
To execute the tests run
How to publish
yarn version --patch yarn version --minor yarn version --major