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.41k stars 45 forks source link

Lookup/computed generics for collection types & more #34

Open kaleidawave opened 1 year ago

kaleidawave commented 1 year ago

Given an array like

let x = [1, 4, "item"]

It is typed (or will be as { [0]: 1, [1]: 4, [2]: "item" }). The problem is how to reference its prototype.

Array is a generic structure so it could be Array<1 | 4 | "item">. However it would have to be modified every push...

It is needed to test equality

let x: Array<number> = [1, 4, "item"]

Alternatively the T type could be figured out from items. Along the lines of interface Array<T is this[number]>...

This also affects Set, Map, which could be done like

interface Map<K is this.#items[number][0], V is this.#items[number][1]> {
    #items: Array<[K, V]>
}

~and also Promise!!! (see #168)~

kaleidawave commented 4 months ago

Won't go into too much detail here but

Still to implement before closing

PatrickLaflamme commented 3 months ago

@kaleidawave - Is this covered in your currently open PRs? If not I'd like to take a stab at it.

kaleidawave commented 3 months ago

Yeah I hopefully will get Set, Map and Promise type structure working in #146. After that this issue should be finished

kaleidawave commented 2 months ago

Didn't happen in #146.

There exists a mechanism called "lookup/computed generics". It works for subtyping and other places.

Lookup generics is manually set, it would be good if it could infer for any array (via trying to find a member, preferable contravariant else covariant for some edge cases).

Will test for Map & Set soon.

Proxy is a different issue #33 and Promises are different #168