assemblyscript icon indicating copy to clipboard operation
assemblyscript copied to clipboard

Trailing comma in type alias causes TS1003 identifier expected compiler error

Open 0xJem opened this issue 3 years ago • 4 comments

Sample code

This causes a TS1003 error on the comma after the third parameter:

export type PriceLookup = (
  tokenAddress: string,
  block: i32,
  currentPool: string,
) => string | null;
ERROR TS1003: Identifier expected.
;;    :
;;  4 │ currentPool: string,
;;    │                    ^
;;    └─ in module.ts(4,22)
;; 
;; ERROR TS1012: Unexpected token.
;;    :
;;  4 │ currentPool: string,
;;    │                    ^
;;    └─ in module.ts(4,22)

This compiles fine:

export type PriceLookup2 = (tokenAddress: string, block: i32, currentPool: string) => string | null;

Functionally, there is no difference between these. However, eslint/prettier will often cause example 2 to be wrapped into the format of example 1, so it would be nice for this to not error.

0xJem avatar Sep 06 '22 09:09 0xJem

Basically problem with trilling comma. This will work:

export type PriceLookup = (
  tokenAddress: string,
  block: i32,
  currentPool: string // removed last comma
) => string | null;

MaxGraey avatar Sep 06 '22 09:09 MaxGraey

Yep, I realise that. It's a common rule in prettier/eslint to add the trailing comma (hence linting doesn't pick up on this). Does the AS compiler (transpiler?) in general report problems with trailing commas? If so, I'll amend our linting rules. It would be nice for that to be mentioned somewhere, though!

0xJem avatar Sep 07 '22 06:09 0xJem

AS supports trailing commas even in cases where TS fails. We just accidentally miss this case.

MaxGraey avatar Sep 07 '22 06:09 MaxGraey

Yes, I just confirmed that trailing commas are generally supported. e.g this works:

  pushArray(
    records,
    getERC20TokenRecordsFromWallets(timestamp, contractAddress, contract, rate, blockNumber),
  );

0xJem avatar Sep 07 '22 08:09 0xJem