element-web icon indicating copy to clipboard operation
element-web copied to clipboard

Update TypeScript target to `es2022` for updated types like `Error.prototype.cause`

Open MadLittleMods opened this issue 2 years ago • 1 comments

Currently, we define the TypeScript target as es2016.

Is there a specific reason, we're staying behind at that version?

Why would you like to update?

I would like to update to es2022 in order get cause property on Error. Introduced in TypeScript 4.6, see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-6.html#--target-es2022

This is spawning from https://github.com/matrix-org/matrix-react-sdk/pull/10432#discussion_r1145538597 where I might have a usage for the cause property and Error constructor usage.

MDN reference for cause property on Error https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause

try {
  connectToDatabase();
} catch (err) {
  throw new Error('Connecting to database failed.', { cause: err });
}

Related TypeScript issues:

  • https://github.com/microsoft/TypeScript/issues/45167
  • https://github.com/microsoft/TypeScript/issues/48098

Possible alternative

For my specific case, we can probably just override what we want in src/@types/global.d.ts#L189-L196

Based off of TypeScript source, src/lib/es2022.error.d.ts

interface ErrorOptions {
    // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
    cause?: unknown;
}

interface Error {
    // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/fileName
    fileName?: string;
    // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/lineNumber
    lineNumber?: number;
    // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/columnNumber
    columnNumber?: number;
    // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause
    cause?: unknown;
}

interface ErrorConstructor {
    new (message?: string, options?: ErrorOptions): Error;
    (message?: string, options?: ErrorOptions): Error;
}

var Error: ErrorConstructor;

MadLittleMods avatar Mar 23 '23 22:03 MadLittleMods

Looking at the compat table at https://compat-table.github.io/compat-table/es2016plus, I can't see any reason not to do this: all the supported platforms for Element-Web, and the recent nodejs versions supported by matrix-js-sdk, are fine all the way up to ES2024, with a yellow flag for "Regexp Matches Indices", which I don't think is relevant here.

richvdh avatar Jun 21 '24 10:06 richvdh

Done

t3chguy avatar Aug 05 '24 15:08 t3chguy