ts-features
v1.3.5
Published
some typescript features like rust
Downloads
3
Readme
TS-Features
This library is for using typescript more comfortable, and bring rust features to typescript!
Usage
installation
Type at your shell
yarn add ts-features
and register transformer to tsconfig.json
{
"compilerOptions": {
"plugins": [
{ "transform": "ts-features/lib/transformer" }
]
}
}
Also, you should use
yarn ttsc
instead of
yarn tsc
pattern-matching
Write interface like this
interface Color {
red: [string];
green: [string];
blue: [string];
}
And, make interface as constant constructor object
const Color = to_enum<Color>();
And, import match function
import { match } from "ts-features";
Finally, you can use pattern matching!
const result = match<string, Color>(Color.Red(["rose"]), {
Red: ([first]) => `${first} red`,
Blue: ([first]) => `${first} blue`,
Green: ([first]) => `${first} green`,
});
expect(result).toBe("rose red");
You can discover samples at tests!
Rust-like error process
You can import these classes
import { Result, Err, Ok } from "ts-features";
And, just use it as if you writing rust code!
const result: Result<{ a: number }, string> = Ok({ a: 1 });
expect(result.isOk).toBe(true);
const result_err: Result<{ a: number }, string> = Err("error");
expect(result_err.isOk).toBe(false);