How can a record be inlined instead of creating a new type?

type address = { street: string, country: string}
type person = { name: string, address: address }

Can the address type be inlined instead of creating a new type so that it looks like the following?

type person = { name: string, address: { street: string, country: string } }

The above type throws compiler error

An inline record type declaration is only allowed in a variant constructor’s declaration

Records types cannot be inlined this way.

The manual contains more information in the record section:

A record type is resolved through finding that single explicit type declaration (we call this “nominal typing”), the type error messages end up better than the counterpart (“structural typing”, like for tuples). This makes refactoring easier; changing a record type’s fields naturally allows the compiler to know that it’s still the same record, just misused in some places. Otherwise, under structural typing, it might get hard to tell whether the definition site or the usage site is wrong.

There is one exception to this rule and that is inside a variant declaration:

type user =
  | Number(int)
  | Id({name: string, password: string})

Would an object type be a good fit for this.

The reason why I was looking for this inline records is it would be easy to migrate from typescript.

type person = {name: string, address: {"street": string, "country": string}}

Both are zero-runtime, so either is fine depending on your situation. It’s possible that you end up changing them from objects to to records afterward anyway, or the other way around. Doesn’t matter!

1 Like