Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
MadLittleMods opened this issue Mar 23, 2023 · 2 comments
Assignees
Labels
A-Developer-Experience A-Testing Testing, code coverage, etc. T-Task Tasks for the team like planning

Comments

@MadLittleMods
Copy link
Contributor

MadLittleMods commented Mar 23, 2023

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 matrix-org/matrix-react-sdk#10432 (comment) 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:

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 MadLittleMods added T-Task Tasks for the team like planning A-Testing Testing, code coverage, etc. A-Developer-Experience labels Mar 23, 2023
@MadLittleMods MadLittleMods changed the title Update TypeScript target to es2022 Update TypeScript target to es2022 for updated types like Error.prototype.cause Mar 23, 2023
@richvdh
Copy link
Member

richvdh commented Jun 21, 2024

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 added a commit to matrix-org/matrix-js-sdk that referenced this issue Jun 21, 2024
I want to be able to use `WeakRef`, and per
element-hq/element-web#24913 (comment),
I believe this should be safe.
github-merge-queue bot pushed a commit to matrix-org/matrix-js-sdk that referenced this issue Jun 21, 2024
* Bump ES target version to ES2022

I want to be able to use `WeakRef`, and per
element-hq/element-web#24913 (comment),
I believe this should be safe.

* room.ts: Fix initialisation order

It seems that ES2022 causes typescript to change the initialization order of
regular properties vs parameter properties
(microsoft/TypeScript#45995), so we need to rearrange
the initializations to avoid an error.

In practice, it might be fine because we have enabled
`babel-plugin-transform-class-properties`, which moves the initialization back
after the parameter property, but we shoudn't rely on that, and anyway it
upsets the linter.
github-merge-queue bot pushed a commit to matrix-org/matrix-js-sdk that referenced this issue Jun 21, 2024
* Bump ES target version to ES2022

I want to be able to use `WeakRef`, and per
element-hq/element-web#24913 (comment),
I believe this should be safe.

* room.ts: Fix initialisation order

It seems that ES2022 causes typescript to change the initialization order of
regular properties vs parameter properties
(microsoft/TypeScript#45995), so we need to rearrange
the initializations to avoid an error.

In practice, it might be fine because we have enabled
`babel-plugin-transform-class-properties`, which moves the initialization back
after the parameter property, but we shoudn't rely on that, and anyway it
upsets the linter.
github-merge-queue bot pushed a commit to matrix-org/matrix-js-sdk that referenced this issue Jun 21, 2024
* Bump ES target version to ES2022

I want to be able to use `WeakRef`, and per
element-hq/element-web#24913 (comment),
I believe this should be safe.

* room.ts: Fix initialisation order

It seems that ES2022 causes typescript to change the initialization order of
regular properties vs parameter properties
(microsoft/TypeScript#45995), so we need to rearrange
the initializations to avoid an error.

In practice, it might be fine because we have enabled
`babel-plugin-transform-class-properties`, which moves the initialization back
after the parameter property, but we shoudn't rely on that, and anyway it
upsets the linter.
@t3chguy t3chguy self-assigned this Aug 1, 2024
@t3chguy
Copy link
Member

t3chguy commented Aug 5, 2024

Done

@t3chguy t3chguy closed this as not planned Won't fix, can't repro, duplicate, stale Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Developer-Experience A-Testing Testing, code coverage, etc. T-Task Tasks for the team like planning
Projects
None yet
Development

No branches or pull requests

3 participants