17

看起来可选链接已经登陆这是一个例子

我想不通的是如何让 TS 正确编译它。我的项目中没有出现任何语法错误,但是:

let imageFileId = (await db.query(sql`select id from image_files where sha256=${sha256}`))[0]?.id;

正在输出为:

let imageFileId = (await db.query(mysql3_1.sql `select id from image_files where sha256=${sha256}`))[0]?.id;

在我们获得 Node.js 的原生支持之前,它不会运行。

这是我的 tsconfig:

{
    "compilerOptions": {
        "strict": true,
        "importHelpers": false,
        "inlineSources": true,
        "noEmitOnError": true,
        "pretty": true,
        "module": "commonjs",
        "noImplicitAny": true,
        "suppressImplicitAnyIndexErrors": false,
        "removeComments": false,
        "preserveConstEnums": false,
        "sourceMap": true,
        "lib": ["es2018"],
        "skipLibCheck": false,
        "outDir": "dist",
        "target": "esnext",
        "declaration": false,
        "resolveJsonModule": true,
        "esModuleInterop": false,
        "moduleResolution": "node",
        "allowSyntheticDefaultImports": true,
        "baseUrl": ".",
        "paths": {
            "*": ["src/*"]
        },
        "noEmit": false
    },
    "files": [
        "src/index"
    ],
    "include": [
        "src/**/*.d.ts"
    ]
}

我需要启用其他选项来编译?.运算符吗?

请注意我没有使用 Babel,我不想把它带入图片中。

4

2 回答 2

22

问题是您的目标是esnext这将告诉编译器按原样输出所有语言功能而无需任何转换。将语言设置为 es2020(或更低),?.并将??被转译为兼容代码:

(async function () {
    let imageFileId = (await db.query(sql`select id from image_files where sha256=${sha256}`))[0]?.id;
})()

游乐场链接

不幸的是,没有对哪些语言功能进行转译以及哪些语言功能没有选择作为一个整体的细粒度控制,

于 2019-11-06T08:20:13.413 回答
5

好吧,我不想使用 Babel,因为那样我就必须弄清楚如何替换ts-node. 那里有一堆过时的文档指的是旧的 Babel 包,但这些说明应该在 2019 年 11 月起生效:

添加一个.babelrc文件:

{
    "presets": [
        ["@babel/preset-env",{"targets": {"node": "current"}}],
        "@babel/preset-typescript"
    ],
    "plugins": [
        "@babel/plugin-syntax-bigint"
    ]
}

添加这些部门:

  "devDependencies": {
    "@babel/cli": "^7.7.0",
    "@babel/core": "^7.7.0",
    "@babel/node": "^7.7.0",
    "@babel/plugin-syntax-bigint": "^7.4.4",
    "@babel/preset-env": "^7.7.1",
    "@babel/preset-typescript": "^7.7.0",
    "@types/node": "^12.7.5",
    "typescript": "^3.7.2"
  }

使用以下命令执行您的代码:

node_modules/.bin/babel-node --extensions ".ts" src/index.ts

--extensions ".ts"非常重要,即使您明确尝试执行 .ts 文件,它也不会在没有它的情况下对其进行转换。

我喜欢使用 GNU Make 而不是 package.json 脚本:

MAKEFLAGS += --no-builtin-rules
.SUFFIXES:
NM := node_modules/.bin
.PHONY: build start dev clean test publish

## commands
########################################

__default:
    $(error Please specify a target)

build: build-types build-js dist/package.json

build-types: node_modules/.yarn-integrity
    $(NM)/tsc --emitDeclarationOnly

build-js: node_modules/.yarn-integrity
    $(NM)/babel src --out-dir dist --extensions ".ts" --source-maps inline

run: node_modules/.yarn-integrity
    $(NM)/babel-node --extensions ".ts" src/index.ts

check: node_modules/.yarn-integrity
    $(NM)/tsc --noEmit

dist:
    mkdir -p $@

clean:
    rm -rf node_modules dist yarn-error.log

dist/package.json: package.json | dist
    jq 'del(.private, .devDependencies, .scripts, .eslintConfig, .babel)' $< > $@

## files
########################################

node_modules/.yarn-integrity: yarn.lock
    @yarn install --frozen-lockfile --production=false --check-files
    @touch -mr $@ $<

yarn.lock: package.json
    @yarn check --integrity
    @touch -mr $@ $<

或者只是从Microsoft 的 TypeScript Babel Starter复制。

于 2019-11-06T08:49:16.907 回答