kaleidawave / ezno

A JavaScript compiler and TypeScript checker written in Rust with a focus on static analysis and runtime performance
https://kaleidawave.github.io/posts/introducing-ezno/
MIT License
2.3k stars 42 forks source link

Nominal classes #128

Open kaleidawave opened 2 months ago

kaleidawave commented 2 months ago

126 adds allowing creating classes as a type which can be referenced

As of #126 they are treated as being nominal. Given

class X { a: number = 2 }

The only two cases of types where instance passes is for

const x: X = instance

is when

Therefore it does not perform property checks for let x: X = { a: 2 } and so will raise a type error. However it only works one way, so let x: { a: number } = new X is fine.

There is also some benefit to the variable_current_value and side effect system which means that shouldn't create to many issues. For let x: { a: number } = new X, x still has the class instance of X type as it doesn't widen.

This can simplify things. In the future:

However TypeScript allows both ways. There may be a way in the future for a CLI flag to check properties (and also drop that for (x: Set<number>) => x.add x.add might not be the Set.prototype.add function)