Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.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编译器?_Typescript_Typescript Compiler Api - Fatal编程技术网

如何在浏览器中使用TypeScript编译器?

如何在浏览器中使用TypeScript编译器?,typescript,typescript-compiler-api,Typescript,Typescript Compiler Api,我正在尝试在浏览器中使用TypeScript编译器(例如Chrome): 上述代码失败,因为createCompilerHost在内部引用了未定义的ts.sys 如何确保ts.sys存在?我能自己模仿吗?分配ts.sys(ts.sys=…)失败,因为它是只读的。这可以通过创建自己的ts.CompilerHost实现来完成: const compilerHost: ts.CompilerHost = { getSourceFile: (fileName: string, languageV

我正在尝试在浏览器中使用TypeScript编译器(例如Chrome):

上述代码失败,因为createCompilerHost在内部引用了未定义的ts.sys


如何确保ts.sys存在?我能自己模仿吗?分配ts.sys(ts.sys=…)失败,因为它是只读的。

这可以通过创建自己的
ts.CompilerHost实现来完成:

const compilerHost: ts.CompilerHost = {
    getSourceFile: (fileName: string, languageVersion: ts.ScriptTarget, onError?: (message: string) => void) => {
        // you will need to implement a way to get source files here
    },
    getDefaultLibFileName: (defaultLibOptions: ts.CompilerOptions) => "/" + ts.getDefaultLibFileName(defaultLibOptions),
    writeFile: () => {}, // do nothing
    getCurrentDirectory: () => "/",
    getDirectories: (path: string) => [],
    fileExists: (fileName: string) => { /* implement this */ },
    readFile: (fileName: string) => { /* implement this */ },
    getCanonicalFileName: (fileName: string) => fileName,
    useCaseSensitiveFileNames: () => true,
    getNewLine: () => "\n",
    getEnvironmentVariable: () => "" // do nothing
};
使用我的库可能更容易,因为它应该在浏览器中工作,并为您加载所有库声明文件。。。如果它不起作用,那么在回购协议的问题上让我知道。要在web上使用它,只需指定使用内存文件系统:

import { Project, ts } from "@ts-morph/bootstrap";

const project = new Project({ useInMemoryFileSystem: true });
// use project here...
import { Project, ts } from "@ts-morph/bootstrap";

const project = new Project({ useInMemoryFileSystem: true });
// use project here...