Coming from TypeScript
A Nish program is a TypeScript file: every line of it is TypeScript syntax. JavaScript decides some things while the program runs: what type a value has, whether an exception unwinds, which method a call reaches. Nish decides all of them when it compiles, so it refuses the constructs that need a run-time decision. This page lists what carries over, what is refused, and what to write instead.
What carries over
Most everyday TypeScript compiles as it is:
interface Point {
x: i32;
y: i32;
}
enum Direction {
Up,
Down,
}
class Counter {
count: i32 = 0;
add(n: i32): void {
this.count = this.count + n;
}
}
const manhattan = (p: Point): i32 => {
const ax = p.x < 0 ? -p.x : p.x;
const ay = p.y < 0 ? -p.y : p.y;
return ax + ay;
};
const step = (d: Direction): i32 => {
switch (d) {
case Direction.Up:
return 1;
case Direction.Down:
return -1;
}
return 0;
};
export const main = (): i32 => {
const points: Point[] = [
{ x: 3, y: -4 },
{ x: -1, y: 2 },
];
const c = new Counter();
for (const p of points) {
c.add(manhattan(p));
}
c.add(step(Direction.Up));
console.log(`total: ${c.count}`);
return 0;
};
That program prints total: 11. It uses const and let, arrow functions,
interfaces, classes with fields and methods, enums, object and array
literals, for...of, switch, the ternary operator and template literals.
Some come with limits: for...of walks an array (or a Map or Set), a
switch is on an integer or an enum (a clause without break falls through
into the next, as in TypeScript; see
switch), and an enum value can only be
compared with === and !==. An enum may be numeric or string; a string
enum's value is not a string, so String(r) gives its text
(Enums). The language
pages give the details.
if, while, do...while, break, continue, === and named import and
export work too. The package ships type declarations for i32, Result and
the other built-in names (runtime/nish.d.ts), so tsc and your editor
understand a Nish file. The reverse does not hold: nish is the authority, as
the reference's It is still
TypeScript
says.
Math carries over too, with JavaScript's answers down to the corner cases —
Math.round(-0.3) is -0, Math.max(1, NaN) is NaN, Math.imul wraps —
but Math.min, max and hypot take exactly two arguments, and the float
functions want an f64, as Types shows.
Functions carry over with more of TypeScript than the program above shows.
A parameter may have a default (by: i32 = 2) and the last one may be a
rest parameter (...xs: i32[]); an arrow written as a call's argument may
read the const locals and parameters around it; async functions and
await compile, as long as each call is awaited where it is made; and an
entry file may skip main and write its statements at the top level. See
Functions,
Async functions and
Your first program.
Each refusal below gives the error code the compiler reports.
number is an integer
In Nish, number means i32, a 32-bit integer, so 7 / 2 is 3. A
fractional value needs f64, the 64-bit floating-point type that a JavaScript
number is:
const ratio: number = 0.5;
const ratio: f64 = 0.5;
The widths are written in the type, and there is one for each machine type:
i32 and i64 for signed integers, u8, u16, u32 and u64 for unsigned
ones, and f32 and f64 for floating point. Two different types never mix in
arithmetic, and nothing converts on its own:
const area = (r: f64): f64 => 3.14159 * r * r;
const total = (n: i32, r: f64): f64 => {
return n * area(r);
};
Convert explicitly with toF64, toI32, toI64, toU8 and the others:
const area = (r: f64): f64 => 3.14159 * r * r;
const total = (n: i32, r: f64): f64 => {
return toF64(n) * area(r);
};
Integer arithmetic is also checked: an i32 or i64 that overflows stops the
program instead of wrapping round, as
Your first program shows. --number-mode f64
makes number mean f64 for a whole program, as in JavaScript; an explicit
i32 or f64 means the same in either mode. See
Types, and the reference's
Types and
Numeric literals.
No any, unknown or union types
Every value has exactly one type, known when the program compiles.
const show = (x: any): void => {
console.log(`${x}`);
};
The same goes for unknown, and for unions such as i32 | string:
const show = (x: i32 | string): void => {
console.log(`${x}`);
};
Name the real type. When a function has to take more than one, write one
function per type. Nish allows two kinds of union. One is T | null, where
T is a class, an interface, an array, a string or a
function reference; the next section covers
it. The other is a union of classes and interfaces that each start with a tag
field, a discriminated union.
undefined is refused in the same way: use null. The one place undefined
appears is in checking the result of Map.get, as in
m.get(key) === undefined.
?. needs a ??
Optional chaining works as long as the chain ends in a default:
class Node {
value: i32 = 0;
next: Node | null = null;
}
const nextValue = (n: Node): i32 => {
return n.next?.value ?? 0;
};
On its own, n.next?.value answers undefined when n.next is null, and
Nish has no undefined value, so a chain without ?? is refused:
class Node {
value: i32 = 0;
next: Node | null = null;
}
const nextValue = (n: Node): i32 => {
const v = n.next?.value;
return 0;
};
When you need more than a default, copy the nullable value into a local and
compare it with null; the compiler narrows it to the non-null type inside
the branch:
class Node {
value: i32 = 0;
next: Node | null = null;
}
const nextValue = (n: Node): i32 => {
const next = n.next;
if (next === null) {
return 0;
}
return next.value;
};
Narrowing works only on a local or a parameter, never on a property path such
as n.next; hence the copy.
?? works on a T | null, on a ?. chain, and on the result of Map.get,
as in counts.get(key) ?? 0. n.next ?? fallback is a Node, with no
narrowing needed. Anywhere else, such as on a number, it is refused
(NL1048), and so is a default for an array element, xs[i] ?? d
(NL2489): past the end of the array, Node answers d where Nish stops the
program. ?.[i] and f?.() are not supported. See the reference's
Nullable types.
No exceptions
There is no throw, no try and no catch:
const half = (n: i32): i32 => {
if (n % 2 !== 0) {
throw new Error("odd");
}
return n / 2;
};
A function that can fail says so in its return type, with Result<T, E>, and
returns Ok(value) or Err(error):
const half = (n: i32): Result<i32, string> => {
if (n % 2 !== 0) {
return Err("odd");
}
return Ok(n / 2);
};
export const main = (): i32 => {
const r = half(7);
if (r.isErr()) {
console.error(r.error);
return 1;
}
console.log(`${r.value}`);
return 0;
};
Seven is odd, so this program prints odd on stderr and exits with 1.
A Result cannot be ignored: a call that returns one cannot stand on its own
as a statement, and reading r.value before checking r.isErr() or
r.isOk() is an error. For a state the program cannot recover from, call
panic("message"), which ends the program the way an overflow does. Errors and Result covers the rest,
including orReturn(), which passes an error up to the caller like Rust's
?. The reference is
Result and error handling.
Classes without inheritance
Classes have fields, one constructor and methods, but no extends:
class Animal {
name: string = "";
}
class Dog extends Animal {
barks: boolean = true;
}
To share a shape, declare it as an interface, and have each class
implements it. The class lists the interface's fields first, in the same
order. Then a function that takes the interface accepts any of those classes:
interface Animal {
name: string;
}
class Dog implements Animal {
name: string;
barks: boolean = true;
constructor(name: string) {
this.name = name;
}
}
class Cat implements Animal {
name: string;
lives: i32 = 9;
constructor(name: string) {
this.name = name;
}
}
const greet = (a: Animal): void => {
console.log(`hello, ${a.name}`);
};
export const main = (): i32 => {
greet(new Dog("Rex"));
greet(new Cat("Tom"));
return 0;
};
An interface holds fields only, never methods: behaviour that several classes
share is a top-level function that takes the interface, like greet above.
There is also no abstract, no static, and no getters or setters. See
Classes and interfaces and the
reference's Classes.
Smaller reflexes
These come up often enough to know before you meet them:
| TypeScript | Nish refuses it with | Write instead |
|---|---|---|
if (xs.length) |
NL2188: a condition must be a boolean |
if (xs.length !== 0) |
a == b |
NL1047: loose equality |
a === b |
xs.reduce(f) |
NL2249: unknown method |
a for...of loop; map, filter, forEach, some, every, findIndex and slice work (Arrays and strings) |
xs.find(f) |
NL2697: find answers T | undefined |
const i = xs.findIndex(f), then xs[i] when i >= 0 |
String(n), n.toString() |
NL2247: unknown function, NL2249: unknown method |
`${n}` |
promise.then(f), Promise.all, a promise in a variable |
NL2650, NL2649, NL2654: a promise is never a value |
await each call where it is made, or awaitAll (Async functions) |
nish --fix rewrites if (xs.length), == and String(n) / n.toString()
for you, as
Your first program
shows.
const xs = [1, 2, 3];
if (xs.length) {
console.log("non-empty");
}
The full list of refused constructs, each with its message and what to write instead, is the reference's Forbidden constructs and Rejected by the checker.

