3 ms·
I'm not an expert in type theory, but something like Scala's path-dependent types might work. In the following example (which I copy-pasted from [1]), every ob
by comex 3y ago
I'm not an expert in type theory, but something like Scala's path-dependent types might work. In the following example (which I copy-pasted from [1]), every object of type Foo has some associated type Bar. You can have multiple objects that are all of type Foo, and they might have different Bar types. Therefore, to refer to a Bar type, you have to reference the specific variable you're talking about – such as in "f1.Bar" below, where f1 is a variable name, not a type name.
trait Foo{
class Bar
def doNothing(b: Bar){}
}
val f1 = new Foo{}
val b1 = new f1.Bar()
val f2 = new Foo{}
val b2 = new f2.Bar()
f1.doNothing(b1) //fine
f1.doNothing(b2) //won't compile
Applying the same idea to TypeScript, you could imagine using 'keyof' on values instead of types. So instead of
keys<T extends object>(o: T): (keyof T)[];
it would be something like
keys(o: object): (keyof o)[];
And the keys returned for an object would only let you index into that specific object, not any other object even if it had the same type.
[1] https://wheaties.github.io/Presentations/Scala-Dep-Types/dependent-types.html https://wheaties.github.io/Presentations/Scala-Dep-Types/dep...