by wwwigham · 5y ago · 0 comments
Fun fact: If you add a private field to a class it'll behave nominally in TS. This is because private fields kinda require nominal relations to function. So, in a way, it does support "switching" to nominal type checking for classes - the opt in is simply per-class.
class CovariantFoo<T> {
private phantom!: T
}
class ContravariantFoo<T> {
private phantom!: (_: T) => void
}
class InvariantFoo<T> {
private phantom!: (_: T) => T
}
(inspired by Rust [0])https://www.cl.cam.ac.uk/~jdy22/papers/lightweight-higher-ki...
[0] https://kotlinlang.org/docs/generics.html#variance
[1] https://kotlinlang.org/docs/generics.html#declaration-site-v...