Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 捆绑类型脚本编译器API_Typescript_Webpack_Typescript Typings - Fatal编程技术网

Typescript 捆绑类型脚本编译器API

Typescript 捆绑类型脚本编译器API,typescript,webpack,typescript-typings,Typescript,Webpack,Typescript Typings,我正在使用在运行时检查接口。由于其他一些依赖关系,我需要最终使用webpack捆绑我的代码,捆绑后的代码不再工作,并且找不到typescript类型 经过一些调查,我发现问题与typescript编译器API有关。在不捆绑的情况下,下面的代码输出节点_模块中找到的所有类型文件,包括typescript模块,捆绑后仍然会找到所有类型文件,但不会找到typescript模块 import * as ts from "typescript"; export async function main()

我正在使用在运行时检查接口。由于其他一些依赖关系,我需要最终使用webpack捆绑我的代码,捆绑后的代码不再工作,并且找不到typescript类型

经过一些调查,我发现问题与typescript编译器API有关。在不捆绑的情况下,下面的代码输出节点_模块中找到的所有类型文件,包括typescript模块,捆绑后仍然会找到所有类型文件,但不会找到typescript模块

import * as ts from "typescript";
export async function main() {
    const p = ts.createProgram({
        rootNames: ["src/dummy.ts"], options: {}
    });
    p.getSourceFiles().forEach(s => console.log(s.fileName));
    console.log(p.getSourceFiles().length);
}
我是否遗漏了一些内容,或者捆绑类型脚本不受支持


webpack.config.js

const path=require('path');
const mode=process.env.NODE\u env==“开发”?“发展”:“生产”;
module.exports={
模式
条目:“./src/app.ts”,
目标:“节点”,
devtool:mode==“开发”?“源映射”:未定义,
节点:{
全球:真的
},
输出:{
path:path.resolve(_dirname,“bin”),
文件名:“bundle.js”,
libraryExport:“默认”
},
决心:{
扩展名:[“.ts”,“.js”]
},
模块:{
规则:[
{
加载器:“ts加载器”,
测试:/\.tsx$/
}
]
}
};
tsconfig.json

{
    "compilerOptions": {
        "moduleResolution": "node",
        "module": "commonjs",
        "target": "ES6",
        "declaration": true,
        "outDir": "bin",
        "resolveJsonModule": true,
        "sourceMap": true
    },
    "include": [
        "src/**/*"
    ],
    "exclude": [
        "bin/**/*.ts",
        "build/*.ts",
        "**/node_modules/**"
    ]
}

编辑:添加输出
在网页前

project/node_modules/typescript/lib/lib.d.ts
project/node_modules/typescript/lib/lib.es5.d.ts
project/node_modules/typescript/lib/lib.es2015.d.ts
<ommitted...>
src/dummy.ts
project/node_modules/@types/json-schema/index.d.ts
project/node_modules/@types/node/globals.d.ts
project/node_modules/@types/node/assert.d.ts
project/node_modules/@types/node/async_hooks.d.ts
project/node_modules/@types/node/buffer.d.ts
<ommitted...>
src/dummy.ts
project/node_modules/@types/json-schema/index.d.ts
project/node_modules/@types/node/globals.d.ts
project/node_modules/@types/node/assert.d.ts
project/node_modules/@types/node/async_hooks.d.ts
project/node_modules/@types/node/buffer.d.ts
<ommitted...>
project/node\u模块/typescript/lib/lib.d.ts
项目/节点\模块/typescript/lib/lib.es5.d.ts
项目/node_模块/typescript/lib/lib.es2015.d.ts
src/dummy.ts
project/node_modules/@types/json schema/index.d.ts
项目/node_modules/@types/node/globals.d.ts
项目/节点\模块/@types/node/assert.d.ts
项目/node_modules/@types/node/async_hooks.d.ts
项目/节点\模块/@types/node/buffer.d.ts
在网页包装之后

project/node_modules/typescript/lib/lib.d.ts
project/node_modules/typescript/lib/lib.es5.d.ts
project/node_modules/typescript/lib/lib.es2015.d.ts
<ommitted...>
src/dummy.ts
project/node_modules/@types/json-schema/index.d.ts
project/node_modules/@types/node/globals.d.ts
project/node_modules/@types/node/assert.d.ts
project/node_modules/@types/node/async_hooks.d.ts
project/node_modules/@types/node/buffer.d.ts
<ommitted...>
src/dummy.ts
project/node_modules/@types/json-schema/index.d.ts
project/node_modules/@types/node/globals.d.ts
project/node_modules/@types/node/assert.d.ts
project/node_modules/@types/node/async_hooks.d.ts
project/node_modules/@types/node/buffer.d.ts
<ommitted...>
src/dummy.ts
project/node_modules/@types/json schema/index.d.ts
项目/node_modules/@types/node/globals.d.ts
项目/节点\模块/@types/node/assert.d.ts
项目/node_modules/@types/node/async_hooks.d.ts
项目/节点\模块/@types/node/buffer.d.ts

我正在使用typescript json模式在运行时检查接口。
我可以问一下为什么吗?听起来您可能有一个。@Kyll我们需要验证TS接口是否匹配后端的JSON配置。编译时既不知道接口也不知道配置。因此,我们希望从接口生成一个模式,并将其与配置进行比较。除了潜在的XY问题外,我还想知道捆绑为什么会改变行为。:)@surrz我没有研究过这一点,但我几乎可以肯定,这与基于成员名称或被破坏的导出名称的推理有关。@AluanHaddad这就是我不太明白的,接口和类型文件都没有捆绑。在webpack之后,TS仍然能够找到json模式、节点和lodash类型,但不能找到诸如Record或Map之类的TS类型。@surrz根据您的编辑,这是我们所期望的。这些文件不是您项目的一部分。由消费者提供这些文件。但是,在开发过程中也是如此。
我正在使用typescript json模式在运行时检查接口。
我可以问一下为什么吗?听起来您可能有一个。@Kyll我们需要验证TS接口是否匹配后端的JSON配置。编译时既不知道接口也不知道配置。因此,我们希望从接口生成一个模式,并将其与配置进行比较。除了潜在的XY问题外,我还想知道捆绑为什么会改变行为。:)@surrz我没有研究过这一点,但我几乎可以肯定,这与基于成员名称或被破坏的导出名称的推理有关。@AluanHaddad这就是我不太明白的,接口和类型文件都没有捆绑。在webpack之后,TS仍然能够找到json模式、节点和lodash类型,但不能找到诸如Record或Map之类的TS类型。@surrz根据您的编辑,这是我们所期望的。这些文件不是您项目的一部分。由消费者提供这些文件。然而,在开发过程中也是如此。