You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Safe Units is a type safe library for using units of measurement in TypeScript. Safe Units provides an implementation of an SI based unit system but is flexible enough to allow users to create their own unit systems which can be independent or can interoperate with the built-in units. Users can also make unit systems for any numeric type they'd like not just the JavaScript `number` type. This library requires TypeScript 3.2 or higher.
6
6
7
-
```typescript
7
+
```ts
8
8
import { Length, Measure, meters, seconds, Time, Velocity } from"safe-units";
9
9
10
10
const length:Length=Measure.of(30, meters);
11
11
const time:Time=Measure.of(15, seconds);
12
12
const velocity:Velocity=length.over(time);
13
13
14
-
console.log(length.toString()); // 30 m
15
-
console.log(time.toString()); // 15 s
14
+
console.log(length.toString()); // 30 m
15
+
console.log(time.toString()); // 15 s
16
16
console.log(velocity.toString()); // 2 m * s^-1
17
17
18
+
// @ts-expect-error ERROR: A measure of m*s isn't assignable to a measure of m/s.
18
19
const error:Velocity=length.times(time);
19
-
// ERROR: A measure of m*s isn't assignable to a measure of m/s.
20
20
```
21
21
22
22
## Features
23
23
24
24
⭐ Compile-time unit arithmetic for typesafe dimensional analysis (with exponents between -5 and +5)!
25
25
26
-
⭐ Large library of predefined units including metric (with prefixes), Imperial, and US customary units!
26
+
⭐ Large library of predefined units including SI (with prefixes), Imperial, and US customary units!
27
27
28
28
⭐ Ability to add your own unit system that can work with built-in units!
Safe Units is a type safe library for using units of measurement in TypeScript. Safe Units provides an implementation of an SI based unit system but is flexible enough to allow users to create their own unit systems which can be independent or can interoperate with the built-in units. Users can also make unit systems for any numeric type they'd like not just the JavaScript `number` type. This library requires TypeScript 3.2 or higher.
6
+
7
+
example: intro.ts
8
+
9
+
## Features
10
+
11
+
⭐ Compile-time unit arithmetic for typesafe dimensional analysis (with exponents between -5 and +5)!
12
+
13
+
⭐ Large library of predefined units including SI (with prefixes), Imperial, and US customary units!
14
+
15
+
⭐ Ability to add your own unit system that can work with built-in units!
16
+
17
+
⭐ Long build times & cryptic error messages!
18
+
19
+
## Prerequisites
20
+
21
+
Safe units is written in TypeScript and should be consumed by TypeScript users to take full advantage of what it provides. In addition you will need the following:
22
+
23
+
-[TypeScript](http://www.typescriptlang.org/) 3.2 or later
24
+
-[Strict null checks](https://www.typescriptlang.org/docs/handbook/compiler-options.html) enabled for your project
0 commit comments