Skip to content

Instantly share code, notes, and snippets.

@kaleidawave
Last active February 2, 2024 13:35
Show Gist options
  • Save kaleidawave/5dcb9ec03deef1161ebf0c9d6e4b88d8 to your computer and use it in GitHub Desktop.
Save kaleidawave/5dcb9ec03deef1161ebf0c9d6e4b88d8 to your computer and use it in GitHub Desktop.

Trying Ezno checking today

This a short overview of some of the things you can do with Ezno today. If you find any problems, file an issue.

The following examples show some errors that have been caught using type checking, partial/const evaluation and effects.

To get started we will install oxidation-compiler which has bindings for Ezno's checker. (you can also get the binary from the releases page).

npm install -g oxidation-compiler@latest

You should now be able to run oxidation-compiler check file.ts. Append the following statements to file.ts and watch the checker do its magic ๐Ÿ”ฎ (the whole script + expected output is at the bottom of the page as well)

Constants

Constants are treated uniquely

const a: 2 = 4;

Operations

Operations on constants use constant functions

const b: 3 = 5 + 2;

Assignments

Assignments modify the value of variables. The type of variables is based on their current value, which can change.

let c = 5;
c = 3;
let d: 2 = c;

Objects and interfaces

Interfaces are declared the same way as in TS

interface Car {
    model: string,
    power: number,
    weight: number
}

Objects can be checked against it

const car1: Car = { model: "Koenigsegg One:1", power: 1360, weight: 1360 }

Properties

Property lookup can find missing names on types

console.lag("log not lag ๐Ÿคฆ")

Also using constant addition the following works

const weight: string = car1["we" + "ight"]

Constant equality

Equality is also a constant function, so unnecessary logic can be found.

if (car1.power === car1.weight) {
    console.log("always here")
}

Functions

Functions with explicit generic parameters can be declared. Using this we can generate a new way to assert something is a type.

function assertType<T>(t: T): void;

Every function parameter is considered generic and so all properties flow through it

function getPerson(name: string) {
    return { name }
}

assertType<{name: "not ben" }>(getPerson("Ben"));

Effects and Try-Catch

some things around effects are currently broken, However this one works

Functions carry effects, which are imperative properties difficult to represent using standard type annotations

function throwValue(value) {
    throw value
}

When calling a function, its effects are run. Therefore the following is caught

try {
    throwValue("my error")
} catch (e) {
    assertType<"different error">(e)
}

That is all for now

Be sure to check out and star ezno as well as oxc if you want to see more!

const a: 2 = 4;
const b: 3 = 5 + 2;
let c = 5;
c = 3;
let d: 2 = c;
interface Car {
model: string,
power: number,
weight: number
}
const car1: Car = { model: "Koenigsegg One:1", power: 1360, weight: 1360 }
console.lag("log not lag ๐Ÿคฆ")
const weight: string = car1["we" + "ight"]
if (car1.power === car1.weight) {
console.log("always here")
}
function assertType<T>(t: T): void {}
function getPerson(name: string) {
return { name }
}
assertType<{name: "not ben" }>(getPerson("Ben"));
function throwValue(value) {
throw value
}
try {
throwValue("my error")
} catch (e) {
assertType<"different error">(e)
}
error:
โ”Œโ”€ .\demo.ts:65:35
โ”‚
65 โ”‚ assertType<"different error">(e)
โ”‚ ----------------- ^ Argument of type "my error" is not assignable to "different error"
โ”‚ โ”‚
โ”‚ Parameter T was specialized with type "different error"
error:
โ”Œโ”€ .\demo.ts:56:32
โ”‚
56 โ”‚ assertType<{name: "not ben" }>(getPerson("Ben"));
โ”‚ ------------------ ^^^^^^^^^^^^^^^^ Argument of type { "name": "Ben", } is not assignable to { "name": "not ben", }
โ”‚ โ”‚
โ”‚ Parameter T was specialized with type { "name": "not ben", }
error:
โ”Œโ”€ .\demo.ts:46:1
โ”‚
46 โ”‚ โ•ญ if (car1.power === car1.weight) {
47 โ”‚ โ”‚ console.log("always here")
48 โ”‚ โ”‚ }
โ”‚ โ•ฐโ”€^ Expression is always true
error:
โ”Œโ”€ .\demo.ts:44:24
โ”‚
44 โ”‚ const weight: string = car1["we" + "ight"]
โ”‚ -------- ^^^^^^^^^^^^^^^^^^^ Type 1360 is not assignable to type string
โ”‚ โ”‚
โ”‚ Variable declared with type string
error:
โ”Œโ”€ .\demo.ts:42:1
โ”‚
42 โ”‚ console.lag("log not lag ๐Ÿคฆ")
โ”‚ ^^^^^^^^^^^ No property with "lag" on Console
error:
โ”Œโ”€ .\demo.ts:32:12
โ”‚
32 โ”‚ let d: 2 = c;
โ”‚ --- ^ Type 3 is not assignable to type 2
โ”‚ โ”‚
โ”‚ Variable declared with type 2
error:
โ”Œโ”€ .\demo.ts:28:14
โ”‚
28 โ”‚ const b: 3 = 5 + 2;
โ”‚ --- ^^^^^ Type 7 is not assignable to type 3
โ”‚ โ”‚
โ”‚ Variable declared with type 3
error:
โ”Œโ”€ .\demo.ts:26:14
โ”‚
26 โ”‚ const a: 2 = 4;
โ”‚ --- ^ Type 4 is not assignable to type 2
โ”‚ โ”‚
โ”‚ Variable declared with type 2
Finished in 5ms.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment