Reduce type duplication in Motoko

Is there any way to extend the type without creating a new type containing similar properties as given in Typescript interfaces?
as an example, I have type like this

public type AuditRecord = {
        fileId: Text;
        action: Action;
        userId: Text;
        timestamp: ?Time.Time;
        organizationId: ?Text;

I wanted to create a new type with additional properties like

public type AuditOrgdto = {
        ...AuditRecord ,
      date: ?Text;

You can use type intersection:

type AuditOrgdto = AuditRecord and { date : ?Text };

I tried that but I’m getting this error

syntax error [M0001], unexpected token 'and', expected one of token or <phrase> sequence:
  ; seplist(<dec_field>,<semicolon>)
  . <id>
  -> <typ>

What version of Motoko are you using? This should work since 0.6.12 from last October.

My dfx version is dfx 0.8.4

Hm, apparently, SDK 0.8.4 still uses Motoko 0.6.11 by default. You should probably update your SDK to version 0.9.

1 Like

I upgrade the SDK. But I’m getting the same error.

It worked. I have to change the version of dfx in dfx.json because I used package vessel package manager. Really thanks for the help @rossberg.

It’s a little strange how I can easily declare a type intersection like this, but I can’t actually construct an object as easily.

For example, there’s no support for:


like there is in JS… right?

Yes, you are right. There is no spread operator in Motoko. But type intersection worked as expected for me.