assemblyscript icon indicating copy to clipboard operation
assemblyscript copied to clipboard

[Bug] Parser gets confused when parsing `null | T`

Open dosisod opened this issue 1 year ago • 2 comments

Given the following AssemblyScript file:

class C {
  x: u8 | null;
  y: null | u8;
}

I get the following errors:

$ asc tmp.ts
ERROR TS1005: 'null' expected.
   :
 3 │ y: null | u8 = null;  // Error, TS1005: 'null' expected.
   │           ~~
   └─ in tmp.ts(3,13)

FAILURE 1 parse error(s)

I would expect both lines to work.

Compiling the following TypeScript code (note the added !'s) it works just fine:

class C {
  x!: number | null;
  y!: null | number;
}

It would seem that | null is only allowed at the end. The solution seems easy enough, seems to be somewhere near src/parser.ts (line 530ish) if I had to guess. I am not familiar with this codebase, but I wouldn't mind making a PR this if need be!

Possible related: #2300

dosisod avatar Aug 20 '22 06:08 dosisod

This y: u8 | null = null doesn't support yet. Only references can be nullable for now.

MaxGraey avatar Aug 20 '22 07:08 MaxGraey

However, this definitely a parser issue:

class C {
  y: null | string;
}

MaxGraey avatar Aug 20 '22 07:08 MaxGraey