小编Lau*_*kik的帖子

与 TS 和 ES2022 进行玩笑 & NodeNext 失败

这是下面的演示代码,用于演示我在当前项目中遇到的失败。

它编译并运行,只是无法使用笑话进行编译,并出现以下错误

错误:错误 TS1343:仅当“--module”选项为“es2020”、“es2022”、“esnext”、“system”、“node16”或“nodenext”时,才允许使用“import.meta”元属性。

文件:src/calc.ts

import { fileURLToPath } from 'url';
import path from 'path';
const __dirname = path.dirname(fileURLToPath(import.meta.url));

export function add(x: number, y: number): number {
  return x + y;
}

export function mul(x: number, y: number): number {
  return x * y;
}
Run Code Online (Sandbox Code Playgroud)

jest.config.cjs:

 module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
};
Run Code Online (Sandbox Code Playgroud)

tsconfig.json:

    {
  "include": [
    "./src/**/*"
  ],
  "exclude": [
    "node_modules"
  ],
  "compilerOptions": {
    "target": "ES2022",
    "module": "NodeNext",
    "outDir": "./out",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": …
Run Code Online (Sandbox Code Playgroud)

typescript jestjs ts-jest

9
推荐指数
1
解决办法
1985
查看次数

JS中的Bigint破坏了数组排序?

好像Array.prototype.sort()坏了BigInt

这有效

const big = [1n, 2n, 3n, 4n];
big.sort();
console.log(big);
// expected output: Array [1n, 2n, 3n, 4n]
Run Code Online (Sandbox Code Playgroud)

但这不是:(

const big = [1n, 2n, 3n, 4n];
big.sort((a,b)=>a-b);
console.log(big);
//Error: Cannot convert a BigInt value to a number
Run Code Online (Sandbox Code Playgroud)

还是我做错了什么?

javascript

5
推荐指数
1
解决办法
402
查看次数

标签 统计

javascript ×1

jestjs ×1

ts-jest ×1

typescript ×1