by DanielHB · 2y ago · 0 comments
class A {
public value: number
}
class B {
public value: number
}
const x: A = new B() // no error
This is structural typing (shape defines type), if typescript had nominal typing (name defines type) this would give an error. You could brand these classes to forcefully cause this to error.Branding makes structural typing work like nominal typing for the branded type only.
It is more like "doing what this article describes" is the default behaviour of most languages (most languages use nominal typing).
Some languages all values are objects and in those languages then the branding argument applies the same way. For languages with nominal typing and primitives you need to box the type yes. Regardless the core of the issue is understanding how structural typing works vs nominal typing
But the compiler can elide the box for you. Haskell and Idris do this.
Haskell's newtype gives a nominal wrapper around a type without (further) boxing at at runtime. It is erased at compile time. Haskell does box their primitives, but via optimization they are used unboxed in some cases (like inside the body of a function). This technique could be applied to a language that doesn't box its primitives.
Idris also does this for any type that is shaped like a newtype (one data constructor, one argument). In that case, both on the scheme and javascript backend, a newtyped Int are represented as "bare" numbers. E.g. with:
type Foo = MkFoo Int
a `MkFoo 5` value is just `5` in the generated javascript or scheme code.https://prosopo.io/articles/typescript-branding/
discussion: https://news.ycombinator.com/item?id=40146751
class A {
private value: number
}
class B {
private value: number
}
const x: A = new B() // error
You can also use the new Javascript private syntax (`#value`). And you can still have public values that are the same, so if you want to force a particular class to have nominal typing, you can add an unused private variable to the class, something like `private __force_nominal!: void`.In fairness, we're also using branded types, which I think is confusing the matter here*. But they are specifically branded nominal types. We can also create structurally-typed brands (before the `unique symbol` technique, that was the only possible option). I think that's what the previous poster was referring to by "simulated nominal typing" — this is distinct from using `unique type` and private members, which are true nominal typing.
* Note: Branded types aren't necessarily a well-defined thing, but for the sake of the discussion let's define them so: a branded type is a type created by adding attributes to a type that exist at compile time but not at runtime.
One part that was not clear to me without testing, and since I do not use typescript regularly, was that you only get nominal typing between the classes that share the private member and if you start going out side that set you lose nominal typing. So you do not get a nominal type, but you can get a subset of types that when interacting with each other act as if they were nominal types.
So class Cat that uses `private __force_nominal!: void` can still be used as class Dog if Dog does not have `private __force_nominal!: void`.
Example[1]:
class Dog {
breed: string
constructor(breed: string) {
this.breed = breed
}
}
function printDog(dog: Dog) {
console.log("Dog: " + dog.breed)
}
class Cat {
private __force_nominal!: string
breed: string
constructor(breed: string) {
this.breed = breed
}
}
const shasta = new Cat("Maine Coon")
printDog(shasta)
edit - the above type checks in typescript 5.4.5[1] modified example from https://asana.com/inside-asana/typescript-quirks
"structural typing" and "nominal typing" are still quite new terms for most devs
That's not quite true. Branding doesn't exist at run time, where as nominal typing usually does at some level. Classes exist at runtime, but most typescript types don't, so unless there's something specific about the shape of the data that you can check with a type guard, it's impossible to narrow the type.
Not necessarily, depending on the language. Functional languages and system languages such as OCaml, Haskell, Rust, but also C (painfully) and C++ can represent wrapper types within a nominal type system at no runtime cost.
https://okmij.org/ftp/Computation/typeclass.html#dict
In Rust, using trait objects also generates a vtable for dynamic dispatch so in that case traits are not fully erased:
https://web.mit.edu/rust-lang_v1.25/arch/amd64_ubuntu1404/sh...