0.0.1-alpha.2
  • 编辑此页(E)
  • 报告 BUG(B)
  • 查看源码(V)
  • 单元测试(U)
  • TypeScriptCompiler

    API

    查看源码
    (共 421 行)
    TypeScriptCompiler 类

    表示一个增量 TypeScript 编译器

    构造函数

    TypeScriptCompiler查看源码
    (共 123 行)
    new TypeScriptCompiler

    初始化新的编译器

    参数
    参数名说明类型
    (可选)options

    附加选项

    展开子属性
    • baseDir: string — 要使用的基路径,将用于查找配置以及依赖的模块
    • configFile: string | string[] — 要读取的 tsconfig.json 路径 - 字符串:指定要加载的配置文件绝对路径 - 数组:指定要加载的配置文件名,将从当前文件夹开始向上搜素同名文件
    • compilerOptions — 编译器的选项,这里设置的选项会覆盖 tsconfig.json 中的同名配置
      • allowJs: boolean
      • allowSyntheticDefaultImports: boolean
      • allowUmdGlobalAccess: boolean
      • allowUnreachableCode: boolean
      • allowUnusedLabels: boolean
      • alwaysStrict: boolean
      • baseUrl: string
      • bundledPackageName: string
      • charset: string
      • checkJs: boolean
      • declaration: boolean
      • declarationMap: boolean
      • emitDeclarationOnly: boolean
      • declarationDir: string
      • disableSizeLimit: boolean
      • disableSourceOfProjectReferenceRedirect: boolean
      • disableSolutionSearching: boolean
      • disableReferencedProjectLoad: boolean
      • downlevelIteration: boolean
      • emitBOM: boolean
      • emitDecoratorMetadata: boolean
      • experimentalDecorators: boolean
      • forceConsistentCasingInFileNames: boolean
      • importHelpers: boolean
      • importsNotUsedAsValues: ImportsNotUsedAsValues
      • inlineSourceMap: boolean
      • inlineSources: boolean
      • isolatedModules: boolean
      • jsx: JsxEmit.None | JsxEmit.Preserve | JsxEmit.React | ... 5 more ...JsxEmit.ReactNative | JsxEmit.ReactJSX | JsxEmit.ReactJSXDev | "none" | "preserve" | "react" | "react-jsx"
      • keyofStringsOnly: boolean
      • lib: string[]
      • locale: string
      • mapRoot: string
      • maxNodeModuleJsDepth: number
      • module: ModuleKind.None | ModuleKind.CommonJS | ModuleKind.AMD | ... 10 more ...ModuleKind.UMD | ModuleKind.System | ModuleKind.ES2015 | ModuleKind.ES2020 | ModuleKind.ESNext | "es6" | "es2015" | "esnext" | "none" | "commonjs" | "system" | "umd"
      • moduleResolution: ModuleResolutionKind.Classic | ModuleResolutionKind.NodeJs | "node" | "classic"
      • newLine: NewLineKind.CarriageReturnLineFeed | NewLineKind.LineFeed | "CRLF" | "LF"
      • noEmit: boolean
      • noEmitHelpers: boolean
      • noEmitOnError: boolean
      • noErrorTruncation: boolean
      • noFallthroughCasesInSwitch: boolean
      • noImplicitAny: boolean
      • noImplicitReturns: boolean
      • noImplicitThis: boolean
      • noStrictGenericChecks: boolean
      • noUnusedLocals: boolean
      • noUnusedParameters: boolean
      • noImplicitUseStrict: boolean
      • noPropertyAccessFromIndexSignature: boolean
      • assumeChangesOnlyAffectDirectDependencies: boolean
      • noLib: boolean
      • noResolve: boolean
      • noUncheckedIndexedAccess: boolean
      • out: string
      • outDir: string
      • outFile: string
      • paths: MapLike<string[]>
      • preserveConstEnums: boolean
      • preserveSymlinks: boolean
      • project: string
      • reactNamespace: string
      • jsxFactory: string
      • jsxFragmentFactory: string
      • jsxImportSource: string
      • composite: boolean
      • incremental: boolean
      • tsBuildInfoFile: string
      • removeComments: boolean
      • rootDir: string
      • rootDirs: string[]
      • skipLibCheck: boolean
      • skipDefaultLibCheck: boolean
      • sourceMap: boolean
      • sourceRoot: string
      • strict: boolean
      • strictFunctionTypes: boolean
      • strictBindCallApply: boolean
      • strictNullChecks: boolean
      • strictPropertyInitialization: boolean
      • stripInternal: boolean
      • suppressExcessPropertyErrors: boolean
      • suppressImplicitAnyIndexErrors: boolean
      • target: string | ScriptTarget.ES3 | ScriptTarget.ES5 | ... 5 more ...ScriptTarget.ES2015 | ScriptTarget.ES2016 | ScriptTarget.ES2017 | ScriptTarget.ES2018 | ScriptTarget.ES2019 | ScriptTarget.ESNext | ScriptTarget.JSON
      • traceResolution: boolean
      • resolveJsonModule: boolean
      • types: string[]
      • typeRoots: string[] — Paths used to compute primary types search locations
      • esModuleInterop: boolean
      • useDefineForClassFields: boolean
    • compilerHost — 自定义编译器宿主对象
      • getSourceFile: typeof getSourceFile
      • getSourceFileByPath: typeof getSourceFileByPath
      • getCancellationToken: typeof getCancellationToken
      • getDefaultLibFileName: typeof getDefaultLibFileName
      • getDefaultLibLocation: typeof getDefaultLibLocation
      • writeFile: WriteFileCallback
      • getCurrentDirectory: typeof getCurrentDirectory
      • getCanonicalFileName: typeof getCanonicalFileName
      • useCaseSensitiveFileNames: typeof useCaseSensitiveFileNames
      • getNewLine: typeof getNewLine
      • readDirectory: typeof readDirectory
      • resolveModuleNames: typeof resolveModuleNames
      • resolveTypeReferenceDirectives: typeof resolveTypeReferenceDirectives — This method is a companion for 'resolveModuleNames' and is used to resolve 'types' references to actual type declaration files
      • getEnvironmentVariable: typeof getEnvironmentVariable
      • createHash: typeof createHash
      • getParsedCommandLine: typeof getParsedCommandLine
      • fileExists: typeof fileExists
      • readFile: typeof readFile
      • trace: typeof trace
      • directoryExists: typeof directoryExists
      • realpath: typeof realpath — Resolve a symbolic link.
      • getDirectories: typeof getDirectories
    • transpileOnly: boolean — 是否仅转换代码但不进行类型检查,启用后将可以大幅提升编译性能
    • renameModuleName: (moduleName: string, sourceFile: SourceFile, program: Program) => string — 自定义重命名模块名的逻辑
    • getCustomTransformers: (program: Program, compiler: TypeScriptCompiler) => CustomTransformers — 获取自定义转换器
    • plugins: array — 自定义转换器插件
      • factory: function(program: Program, compiler: (Circular)TypeScriptCompilerPlugin) => CustomTransformers | (TransformerFactory<SourceFile> | CustomTransformerFactory)[] — 返回转换器的工厂函数
      • transform: string — 插件模块名
      • import: string — 从插件模块中导入的名称
      • type: "compilerOptions" | "program" | "raw" | "config" | "checker" — 从插件模块中导入的类型
      • after: boolean — 是否应该在内置 JS 转换器之后执行转换器
      • afterDeclarations: boolean — 是否应该在生成 .d.ts 文件之后执行转换器

    默认值:{ }

    TypeScriptCompilerOptions

    属性

    属性名说明类型
    baseDir只读

    获取基路径

    string
    configFilePaths只读

    获取已读取的配置文件路径

    Set<string>
    compilerOptions只读

    获取全局默认的编译器选项

    展开子属性
    • allowJs: boolean
    • allowSyntheticDefaultImports: boolean
    • allowUmdGlobalAccess: boolean
    • allowUnreachableCode: boolean
    • allowUnusedLabels: boolean
    • alwaysStrict: boolean
    • baseUrl: string
    • bundledPackageName: string
    • charset: string
    • checkJs: boolean
    • declaration: boolean
    • declarationMap: boolean
    • emitDeclarationOnly: boolean
    • declarationDir: string
    • disableSizeLimit: boolean
    • disableSourceOfProjectReferenceRedirect: boolean
    • disableSolutionSearching: boolean
    • disableReferencedProjectLoad: boolean
    • downlevelIteration: boolean
    • emitBOM: boolean
    • emitDecoratorMetadata: boolean
    • experimentalDecorators: boolean
    • forceConsistentCasingInFileNames: boolean
    • importHelpers: boolean
    • importsNotUsedAsValues: ImportsNotUsedAsValues
    • inlineSourceMap: boolean
    • inlineSources: boolean
    • isolatedModules: boolean
    • jsx: JsxEmit
    • keyofStringsOnly: boolean
    • lib: string[]
    • locale: string
    • mapRoot: string
    • maxNodeModuleJsDepth: number
    • module: ModuleKind
    • moduleResolution: ModuleResolutionKind
    • newLine: NewLineKind
    • noEmit: boolean
    • noEmitHelpers: boolean
    • noEmitOnError: boolean
    • noErrorTruncation: boolean
    • noFallthroughCasesInSwitch: boolean
    • noImplicitAny: boolean
    • noImplicitReturns: boolean
    • noImplicitThis: boolean
    • noStrictGenericChecks: boolean
    • noUnusedLocals: boolean
    • noUnusedParameters: boolean
    • noImplicitUseStrict: boolean
    • noPropertyAccessFromIndexSignature: boolean
    • assumeChangesOnlyAffectDirectDependencies: boolean
    • noLib: boolean
    • noResolve: boolean
    • noUncheckedIndexedAccess: boolean
    • out: string
    • outDir: string
    • outFile: string
    • paths: MapLike<string[]>
    • preserveConstEnums: boolean
    • preserveSymlinks: boolean
    • project: string
    • reactNamespace: string
    • jsxFactory: string
    • jsxFragmentFactory: string
    • jsxImportSource: string
    • composite: boolean
    • incremental: boolean
    • tsBuildInfoFile: string
    • removeComments: boolean
    • rootDir: string
    • rootDirs: string[]
    • skipLibCheck: boolean
    • skipDefaultLibCheck: boolean
    • sourceMap: boolean
    • sourceRoot: string
    • strict: boolean
    • strictFunctionTypes: boolean
    • strictBindCallApply: boolean
    • strictNullChecks: boolean
    • strictPropertyInitialization: boolean
    • stripInternal: boolean
    • suppressExcessPropertyErrors: boolean
    • suppressImplicitAnyIndexErrors: boolean
    • target: ScriptTarget
    • traceResolution: boolean
    • resolveJsonModule: boolean
    • types: string[]
    • typeRoots: string[] — Paths used to compute primary types search locations
    • esModuleInterop: boolean
    • useDefineForClassFields: boolean
    CompilerOptions
    compilerHost只读

    获取使用的 TypeScript 编译器宿主对象

    CompilerHost
    configFileNames(可选)只读

    获取在每个文件夹读取的配置文件名,默认为 [tsconfig.json, jsconfig.json]

    string[]
    plugins(可选)

    获取自定义的插件

    展开子属性
    • factory: (program: Program, compiler: (Circular)TypeScriptCompilerPlugin) => CustomTransformers | (TransformerFactory<SourceFile> | CustomTransformerFactory)[] — 返回转换器的工厂函数
    • transform: string — 插件模块名
    • import: string — 从插件模块中导入的名称
    • type: "compilerOptions" | "program" | "raw" | "config" | "checker" — 从插件模块中导入的类型
    • after: boolean — 是否应该在内置 JS 转换器之后执行转换器
    • afterDeclarations: boolean — 是否应该在生成 .d.ts 文件之后执行转换器
    TypeScriptCompilerPlugin[]
    renameModuleName(可选)

    获取自定义模块重命名函数

    (moduleName: string, sourceFile: SourceFile, program: Program) => string

    方法

    参数
    参数名说明类型
    plugin

    要载入的插件

    展开子属性
    • factory: (program: Program, compiler: (Circular)TypeScriptCompilerPlugin) => CustomTransformers | (TransformerFactory<SourceFile> | CustomTransformerFactory)[] — 返回转换器的工厂函数
    • transform: string — 插件模块名
    • import: string — 从插件模块中导入的名称
    • type: "compilerOptions" | "program" | "raw" | "config" | "checker" — 从插件模块中导入的类型
    • after: boolean — 是否应该在内置 JS 转换器之后执行转换器
    • afterDeclarations: boolean — 是否应该在生成 .d.ts 文件之后执行转换器
    TypeScriptCompilerPlugin
    返回值
    类型:void

    TypeScriptCompiler查看源码
    (共 47 行)
    compile

    编译一段 TypeScript 代码到 JavaScript

    参数
    参数名说明类型
    content

    要编译的源码,如果未指定则根据路径读取

    string
    (可选)fileName

    源码的路径,用于查找和该路径相关的配置以及解析源码中导入的相对路径,要编译 JSX,文件名必须以 x 结尾

    string
    (可选)checkErrors

    是否检查错误,如果为 transpileOnly 则只检查语法错误,忽略类型错误

    默认值:"transpileOnly";

    false | true | "transpileOnly"
    (可选)moduleName

    如果要生成 AMD 模块,则指定当前模块名

    string
    (可选)renameModuleName

    重命名导入模块名的回调函数

    默认值:this.renameModuleName;

    (moduleName: string, sourceFile: SourceFile, program: Program) => string
    (可选)options

    针对当前文件的编译器选项

    展开子属性
    • allowJs: boolean
    • allowSyntheticDefaultImports: boolean
    • allowUmdGlobalAccess: boolean
    • allowUnreachableCode: boolean
    • allowUnusedLabels: boolean
    • alwaysStrict: boolean
    • baseUrl: string
    • bundledPackageName: string
    • charset: string
    • checkJs: boolean
    • declaration: boolean
    • declarationMap: boolean
    • emitDeclarationOnly: boolean
    • declarationDir: string
    • disableSizeLimit: boolean
    • disableSourceOfProjectReferenceRedirect: boolean
    • disableSolutionSearching: boolean
    • disableReferencedProjectLoad: boolean
    • downlevelIteration: boolean
    • emitBOM: boolean
    • emitDecoratorMetadata: boolean
    • experimentalDecorators: boolean
    • forceConsistentCasingInFileNames: boolean
    • importHelpers: boolean
    • importsNotUsedAsValues: ImportsNotUsedAsValues
    • inlineSourceMap: boolean
    • inlineSources: boolean
    • isolatedModules: boolean
    • jsx: JsxEmit.None | JsxEmit.Preserve | JsxEmit.React | ... 5 more ...JsxEmit.ReactNative | JsxEmit.ReactJSX | JsxEmit.ReactJSXDev | "none" | "preserve" | "react" | "react-jsx"
    • keyofStringsOnly: boolean
    • lib: string[]
    • locale: string
    • mapRoot: string
    • maxNodeModuleJsDepth: number
    • module: ModuleKind.None | ModuleKind.CommonJS | ModuleKind.AMD | ... 10 more ...ModuleKind.UMD | ModuleKind.System | ModuleKind.ES2015 | ModuleKind.ES2020 | ModuleKind.ESNext | "es6" | "es2015" | "esnext" | "none" | "commonjs" | "system" | "umd"
    • moduleResolution: ModuleResolutionKind.Classic | ModuleResolutionKind.NodeJs | "node" | "classic"
    • newLine: NewLineKind.CarriageReturnLineFeed | NewLineKind.LineFeed | "CRLF" | "LF"
    • noEmit: boolean
    • noEmitHelpers: boolean
    • noEmitOnError: boolean
    • noErrorTruncation: boolean
    • noFallthroughCasesInSwitch: boolean
    • noImplicitAny: boolean
    • noImplicitReturns: boolean
    • noImplicitThis: boolean
    • noStrictGenericChecks: boolean
    • noUnusedLocals: boolean
    • noUnusedParameters: boolean
    • noImplicitUseStrict: boolean
    • noPropertyAccessFromIndexSignature: boolean
    • assumeChangesOnlyAffectDirectDependencies: boolean
    • noLib: boolean
    • noResolve: boolean
    • noUncheckedIndexedAccess: boolean
    • out: string
    • outDir: string
    • outFile: string
    • paths: MapLike<string[]>
    • preserveConstEnums: boolean
    • preserveSymlinks: boolean
    • project: string
    • reactNamespace: string
    • jsxFactory: string
    • jsxFragmentFactory: string
    • jsxImportSource: string
    • composite: boolean
    • incremental: boolean
    • tsBuildInfoFile: string
    • removeComments: boolean
    • rootDir: string
    • rootDirs: string[]
    • skipLibCheck: boolean
    • skipDefaultLibCheck: boolean
    • sourceMap: boolean
    • sourceRoot: string
    • strict: boolean
    • strictFunctionTypes: boolean
    • strictBindCallApply: boolean
    • strictNullChecks: boolean
    • strictPropertyInitialization: boolean
    • stripInternal: boolean
    • suppressExcessPropertyErrors: boolean
    • suppressImplicitAnyIndexErrors: boolean
    • target: string | ScriptTarget.ES3 | ScriptTarget.ES5 | ... 5 more ...ScriptTarget.ES2015 | ScriptTarget.ES2016 | ScriptTarget.ES2017 | ScriptTarget.ES2018 | ScriptTarget.ES2019 | ScriptTarget.ESNext | ScriptTarget.JSON
    • traceResolution: boolean
    • resolveJsonModule: boolean
    • types: string[]
    • typeRoots: string[] — Paths used to compute primary types search locations
    • esModuleInterop: boolean
    • useDefineForClassFields: boolean
    Merge<CompilerOptions, {target?: stringmodule?: "es6" | "es2015" | "esnext" | "none" | "commonjs" | "amd" | "system" | "umd"jsx?: "none" | "preserve" | "react-native" | "react" | "react-jsx"moduleResolution?: "node" | "classic"newLine?: "CRLF" | "LF"}>
    返回值

    TypeScriptCompiler查看源码
    (共 30 行)
    createProgram

    创建用于编译指定文件的工程对象

    参数
    参数名说明类型
    fileName

    文件的路径,如果路径为空,则文件内容必须不能为空

    string
    (可选)content

    文件的内容,如果未指定则根据路径读取

    string
    (可选)options

    针对当前文件的编译器选项

    展开子属性
    • allowJs: boolean
    • allowSyntheticDefaultImports: boolean
    • allowUmdGlobalAccess: boolean
    • allowUnreachableCode: boolean
    • allowUnusedLabels: boolean
    • alwaysStrict: boolean
    • baseUrl: string
    • bundledPackageName: string
    • charset: string
    • checkJs: boolean
    • declaration: boolean
    • declarationMap: boolean
    • emitDeclarationOnly: boolean
    • declarationDir: string
    • disableSizeLimit: boolean
    • disableSourceOfProjectReferenceRedirect: boolean
    • disableSolutionSearching: boolean
    • disableReferencedProjectLoad: boolean
    • downlevelIteration: boolean
    • emitBOM: boolean
    • emitDecoratorMetadata: boolean
    • experimentalDecorators: boolean
    • forceConsistentCasingInFileNames: boolean
    • importHelpers: boolean
    • importsNotUsedAsValues: ImportsNotUsedAsValues
    • inlineSourceMap: boolean
    • inlineSources: boolean
    • isolatedModules: boolean
    • jsx: JsxEmit.None | JsxEmit.Preserve | JsxEmit.React | ... 5 more ...JsxEmit.ReactNative | JsxEmit.ReactJSX | JsxEmit.ReactJSXDev | "none" | "preserve" | "react" | "react-jsx"
    • keyofStringsOnly: boolean
    • lib: string[]
    • locale: string
    • mapRoot: string
    • maxNodeModuleJsDepth: number
    • module: ModuleKind.None | ModuleKind.CommonJS | ModuleKind.AMD | ... 10 more ...ModuleKind.UMD | ModuleKind.System | ModuleKind.ES2015 | ModuleKind.ES2020 | ModuleKind.ESNext | "es6" | "es2015" | "esnext" | "none" | "commonjs" | "system" | "umd"
    • moduleResolution: ModuleResolutionKind.Classic | ModuleResolutionKind.NodeJs | "node" | "classic"
    • newLine: NewLineKind.CarriageReturnLineFeed | NewLineKind.LineFeed | "CRLF" | "LF"
    • noEmit: boolean
    • noEmitHelpers: boolean
    • noEmitOnError: boolean
    • noErrorTruncation: boolean
    • noFallthroughCasesInSwitch: boolean
    • noImplicitAny: boolean
    • noImplicitReturns: boolean
    • noImplicitThis: boolean
    • noStrictGenericChecks: boolean
    • noUnusedLocals: boolean
    • noUnusedParameters: boolean
    • noImplicitUseStrict: boolean
    • noPropertyAccessFromIndexSignature: boolean
    • assumeChangesOnlyAffectDirectDependencies: boolean
    • noLib: boolean
    • noResolve: boolean
    • noUncheckedIndexedAccess: boolean
    • out: string
    • outDir: string
    • outFile: string
    • paths: MapLike<string[]>
    • preserveConstEnums: boolean
    • preserveSymlinks: boolean
    • project: string
    • reactNamespace: string
    • jsxFactory: string
    • jsxFragmentFactory: string
    • jsxImportSource: string
    • composite: boolean
    • incremental: boolean
    • tsBuildInfoFile: string
    • removeComments: boolean
    • rootDir: string
    • rootDirs: string[]
    • skipLibCheck: boolean
    • skipDefaultLibCheck: boolean
    • sourceMap: boolean
    • sourceRoot: string
    • strict: boolean
    • strictFunctionTypes: boolean
    • strictBindCallApply: boolean
    • strictNullChecks: boolean
    • strictPropertyInitialization: boolean
    • stripInternal: boolean
    • suppressExcessPropertyErrors: boolean
    • suppressImplicitAnyIndexErrors: boolean
    • target: string | ScriptTarget.ES3 | ScriptTarget.ES5 | ... 5 more ...ScriptTarget.ES2015 | ScriptTarget.ES2016 | ScriptTarget.ES2017 | ScriptTarget.ES2018 | ScriptTarget.ES2019 | ScriptTarget.ESNext | ScriptTarget.JSON
    • traceResolution: boolean
    • resolveJsonModule: boolean
    • types: string[]
    • typeRoots: string[] — Paths used to compute primary types search locations
    • esModuleInterop: boolean
    • useDefineForClassFields: boolean
    Merge<CompilerOptions, {target?: stringmodule?: "es6" | "es2015" | "esnext" | "none" | "commonjs" | "amd" | "system" | "umd"jsx?: "none" | "preserve" | "react-native" | "react" | "react-jsx"moduleResolution?: "node" | "classic"newLine?: "CRLF" | "LF"}>
    返回值
    类型:Program

    TypeScriptCompiler查看源码
    (共 7 行)
    toPath

    返回内部使用的完整绝对路径

    参数
    参数名说明类型
    fileName

    原始文件名

    string
    返回值
    类型:Path

    TypeScriptCompiler查看源码
    (共 29 行)
    getCompilerOptions

    获取指定路径对应的编译器选项

    参数
    参数名说明类型
    path

    要查找的路径

    string
    返回值
    类型:CompilerOptions

    TypeScriptCompiler查看源码
    (共 29 行)
    getCustomTransformers

    获取自定义转换器,如果转换器为空则返回 undefined

    参数
    参数名说明类型
    program

    当前要转换的程序

    Program
    返回值
    类型:CustomTransformers

    TypeScriptCompiler查看源码
    (共 16 行)
    emitUpdate

    通知指定的文件已更新

    参数
    参数名说明类型
    path

    修改的文件绝对路径

    string
    返回值
    类型:void

    TypeScriptCompiler查看源码
    (共 7 行)
    formatErrors

    格式化诊断信息

    参数
    参数名说明类型
    errors

    要格式化的对象

    展开子属性
    • reportsUnnecessary: {} — May store more in future. For now, this will simply be true to indicate when a diagnostic is an unused-identifier diagnostic.
    • reportsDeprecated: {}
    • source: string
    • relatedInformation: array
      • category: DiagnosticCategory
      • code: number
      • file: SourceFile
      • start: number
      • length: number
      • messageText
        • messageText: string
        • category: DiagnosticCategory
        • code: number
        • next: (Circular)DiagnosticMessageChain[]
    • category: DiagnosticCategory
    • code: number
    • file: SourceFile
    • start: number
    • length: number
    • messageText
      • messageText: string
      • category: DiagnosticCategory
      • code: number
      • next: (Circular)DiagnosticMessageChain[]
    Diagnostic[]
    返回值
    类型:string

    TypeScriptCompiler查看源码
    (共 7 行)
    getSourceFile

    获取指定路径对应的源文件对象

    参数
    参数名说明类型
    fileName

    文件的路径

    string
    返回值
    类型:SourceFile

    TypeScriptCompiler查看源码
    (共 4 行)
    getCurrentProgram

    获取上一次编译时创建的工程对象

    返回值
    类型:Program

    TypeScriptCompiler查看源码
    (共 10 行)
    resolveModuleName

    使用内置的解析规则解析模块

    参数
    参数名说明类型
    moduleName

    要解析的模块名

    string
    containingFile

    所在的文件

    string
    (可选)redirectedReference

    重定向的引用

    展开子属性
    • commandLine
      • options
        • allowJs: boolean
        • allowSyntheticDefaultImports: boolean
        • allowUmdGlobalAccess: boolean
        • allowUnreachableCode: boolean
        • allowUnusedLabels: boolean
        • alwaysStrict: boolean
        • baseUrl: string
        • bundledPackageName: string
        • charset: string
        • checkJs: boolean
        • declaration: boolean
        • declarationMap: boolean
        • emitDeclarationOnly: boolean
        • declarationDir: string
        • disableSizeLimit: boolean
        • disableSourceOfProjectReferenceRedirect: boolean
        • disableSolutionSearching: boolean
        • disableReferencedProjectLoad: boolean
        • downlevelIteration: boolean
        • emitBOM: boolean
        • emitDecoratorMetadata: boolean
        • experimentalDecorators: boolean
        • forceConsistentCasingInFileNames: boolean
        • importHelpers: boolean
        • importsNotUsedAsValues: ImportsNotUsedAsValues
        • inlineSourceMap: boolean
        • inlineSources: boolean
        • isolatedModules: boolean
        • jsx: JsxEmit
        • keyofStringsOnly: boolean
        • lib: string[]
        • locale: string
        • mapRoot: string
        • maxNodeModuleJsDepth: number
        • module: ModuleKind
        • moduleResolution: ModuleResolutionKind
        • newLine: NewLineKind
        • noEmit: boolean
        • noEmitHelpers: boolean
        • noEmitOnError: boolean
        • noErrorTruncation: boolean
        • noFallthroughCasesInSwitch: boolean
        • noImplicitAny: boolean
        • noImplicitReturns: boolean
        • noImplicitThis: boolean
        • noStrictGenericChecks: boolean
        • noUnusedLocals: boolean
        • noUnusedParameters: boolean
        • noImplicitUseStrict: boolean
        • noPropertyAccessFromIndexSignature: boolean
        • assumeChangesOnlyAffectDirectDependencies: boolean
        • noLib: boolean
        • noResolve: boolean
        • noUncheckedIndexedAccess: boolean
        • out: string
        • outDir: string
        • outFile: string
        • paths: MapLike<string[]>
        • preserveConstEnums: boolean
        • preserveSymlinks: boolean
        • project: string
        • reactNamespace: string
        • jsxFactory: string
        • jsxFragmentFactory: string
        • jsxImportSource: string
        • composite: boolean
        • incremental: boolean
        • tsBuildInfoFile: string
        • removeComments: boolean
        • rootDir: string
        • rootDirs: string[]
        • skipLibCheck: boolean
        • skipDefaultLibCheck: boolean
        • sourceMap: boolean
        • sourceRoot: string
        • strict: boolean
        • strictFunctionTypes: boolean
        • strictBindCallApply: boolean
        • strictNullChecks: boolean
        • strictPropertyInitialization: boolean
        • stripInternal: boolean
        • suppressExcessPropertyErrors: boolean
        • suppressImplicitAnyIndexErrors: boolean
        • target: ScriptTarget
        • traceResolution: boolean
        • resolveJsonModule: boolean
        • types: string[]
        • typeRoots: string[] — Paths used to compute primary types search locations
        • esModuleInterop: boolean
        • useDefineForClassFields: boolean
      • typeAcquisition
        • enableAutoDiscovery: boolean
        • enable: boolean
        • include: string[]
        • exclude: string[]
        • disableFilenameBasedTypeAcquisition: boolean
      • fileNames: string[]
      • projectReferences: array
        • path: string — A normalized path on disk
        • originalPath: string — The path as the user originally wrote it
        • prepend: boolean — True if the output of this reference should be prepended to the output of this project. Only valid for --outFile compilations
        • circular: boolean — True if it is intended that this reference form a circularity
      • watchOptions
        • watchFile: WatchFileKind
        • watchDirectory: WatchDirectoryKind
        • fallbackPolling: PollingWatchKind
        • synchronousWatchDirectory: boolean
        • excludeDirectories: string[]
        • excludeFiles: string[]
      • raw: any
      • errors: array
        • reportsUnnecessary: {} — May store more in future. For now, this will simply be true to indicate when a diagnostic is an unused-identifier diagnostic.
        • reportsDeprecated: {}
        • source: string
        • relatedInformation: array
          • category: DiagnosticCategory
          • code: number
          • file: SourceFile
          • start: number
          • length: number
          • messageText
            • messageText: string
            • category: DiagnosticCategory
            • code: number
            • next: (Circular)DiagnosticMessageChain[]
        • category: DiagnosticCategory
        • code: number
        • file: SourceFile
        • start: number
        • length: number
        • messageText
          • messageText: string
          • category: DiagnosticCategory
          • code: number
          • next: (Circular)DiagnosticMessageChain[]
      • wildcardDirectories: MapLike<WatchDirectoryFlags>
      • compileOnSave: boolean
    • sourceFile: SourceFile
    • references: (Circular)ResolvedProjectReference[]
    ResolvedProjectReference
    返回值
    类型:ResolvedModuleWithFailedLookupLocations

    TypeScriptCompiler查看源码
    (共 8 行)
    resolveJSModuleName

    使用内置的解析规则解析 JS 模块

    参数
    参数名说明类型
    moduleName

    要解析的模块名

    string
    containingFile

    所在的文件

    string
    返回值
    类型:string

    查看源码
    (共 33 行)
    TypeScriptCompilerOptions 接口

    表示 TypeScript 编译器的附加选项

    属性

    属性名说明类型
    baseDir

    要使用的基路径,将用于查找配置以及依赖的模块

    string
    configFile

    要读取的 tsconfig.json 路径

    • 字符串:指定要加载的配置文件绝对路径
    • 数组:指定要加载的配置文件名,将从当前文件夹开始向上搜素同名文件
    string | string[]
    compilerOptions

    编译器的选项,这里设置的选项会覆盖 tsconfig.json 中的同名配置

    展开子属性
    • allowJs: boolean
    • allowSyntheticDefaultImports: boolean
    • allowUmdGlobalAccess: boolean
    • allowUnreachableCode: boolean
    • allowUnusedLabels: boolean
    • alwaysStrict: boolean
    • baseUrl: string
    • bundledPackageName: string
    • charset: string
    • checkJs: boolean
    • declaration: boolean
    • declarationMap: boolean
    • emitDeclarationOnly: boolean
    • declarationDir: string
    • disableSizeLimit: boolean
    • disableSourceOfProjectReferenceRedirect: boolean
    • disableSolutionSearching: boolean
    • disableReferencedProjectLoad: boolean
    • downlevelIteration: boolean
    • emitBOM: boolean
    • emitDecoratorMetadata: boolean
    • experimentalDecorators: boolean
    • forceConsistentCasingInFileNames: boolean
    • importHelpers: boolean
    • importsNotUsedAsValues: ImportsNotUsedAsValues
    • inlineSourceMap: boolean
    • inlineSources: boolean
    • isolatedModules: boolean
    • jsx: JsxEmit.None | JsxEmit.Preserve | JsxEmit.React | ... 5 more ...JsxEmit.ReactNative | JsxEmit.ReactJSX | JsxEmit.ReactJSXDev | "none" | "preserve" | "react" | "react-jsx"
    • keyofStringsOnly: boolean
    • lib: string[]
    • locale: string
    • mapRoot: string
    • maxNodeModuleJsDepth: number
    • module: ModuleKind.None | ModuleKind.CommonJS | ModuleKind.AMD | ... 10 more ...ModuleKind.UMD | ModuleKind.System | ModuleKind.ES2015 | ModuleKind.ES2020 | ModuleKind.ESNext | "es6" | "es2015" | "esnext" | "none" | "commonjs" | "system" | "umd"
    • moduleResolution: ModuleResolutionKind.Classic | ModuleResolutionKind.NodeJs | "node" | "classic"
    • newLine: NewLineKind.CarriageReturnLineFeed | NewLineKind.LineFeed | "CRLF" | "LF"
    • noEmit: boolean
    • noEmitHelpers: boolean
    • noEmitOnError: boolean
    • noErrorTruncation: boolean
    • noFallthroughCasesInSwitch: boolean
    • noImplicitAny: boolean
    • noImplicitReturns: boolean
    • noImplicitThis: boolean
    • noStrictGenericChecks: boolean
    • noUnusedLocals: boolean
    • noUnusedParameters: boolean
    • noImplicitUseStrict: boolean
    • noPropertyAccessFromIndexSignature: boolean
    • assumeChangesOnlyAffectDirectDependencies: boolean
    • noLib: boolean
    • noResolve: boolean
    • noUncheckedIndexedAccess: boolean
    • out: string
    • outDir: string
    • outFile: string
    • paths: MapLike<string[]>
    • preserveConstEnums: boolean
    • preserveSymlinks: boolean
    • project: string
    • reactNamespace: string
    • jsxFactory: string
    • jsxFragmentFactory: string
    • jsxImportSource: string
    • composite: boolean
    • incremental: boolean
    • tsBuildInfoFile: string
    • removeComments: boolean
    • rootDir: string
    • rootDirs: string[]
    • skipLibCheck: boolean
    • skipDefaultLibCheck: boolean
    • sourceMap: boolean
    • sourceRoot: string
    • strict: boolean
    • strictFunctionTypes: boolean
    • strictBindCallApply: boolean
    • strictNullChecks: boolean
    • strictPropertyInitialization: boolean
    • stripInternal: boolean
    • suppressExcessPropertyErrors: boolean
    • suppressImplicitAnyIndexErrors: boolean
    • target: string | ScriptTarget.ES3 | ScriptTarget.ES5 | ... 5 more ...ScriptTarget.ES2015 | ScriptTarget.ES2016 | ScriptTarget.ES2017 | ScriptTarget.ES2018 | ScriptTarget.ES2019 | ScriptTarget.ESNext | ScriptTarget.JSON
    • traceResolution: boolean
    • resolveJsonModule: boolean
    • types: string[]
    • typeRoots: string[] — Paths used to compute primary types search locations
    • esModuleInterop: boolean
    • useDefineForClassFields: boolean
    Merge<CompilerOptions, {target?: stringmodule?: "es6" | "es2015" | "esnext" | "none" | "commonjs" | "amd" | "system" | "umd"jsx?: "none" | "preserve" | "react-native" | "react" | "react-jsx"moduleResolution?: "node" | "classic"newLine?: "CRLF" | "LF"}>
    compilerHost

    自定义编译器宿主对象

    展开子属性
    • getSourceFile: typeof getSourceFile
    • getSourceFileByPath: typeof getSourceFileByPath
    • getCancellationToken: typeof getCancellationToken
    • getDefaultLibFileName: typeof getDefaultLibFileName
    • getDefaultLibLocation: typeof getDefaultLibLocation
    • writeFile: WriteFileCallback
    • getCurrentDirectory: typeof getCurrentDirectory
    • getCanonicalFileName: typeof getCanonicalFileName
    • useCaseSensitiveFileNames: typeof useCaseSensitiveFileNames
    • getNewLine: typeof getNewLine
    • readDirectory: typeof readDirectory
    • resolveModuleNames: typeof resolveModuleNames
    • resolveTypeReferenceDirectives: typeof resolveTypeReferenceDirectives — This method is a companion for 'resolveModuleNames' and is used to resolve 'types' references to actual type declaration files
    • getEnvironmentVariable: typeof getEnvironmentVariable
    • createHash: typeof createHash
    • getParsedCommandLine: typeof getParsedCommandLine
    • fileExists: typeof fileExists
    • readFile: typeof readFile
    • trace: typeof trace
    • directoryExists: typeof directoryExists
    • realpath: typeof realpath — Resolve a symbolic link.
    • getDirectories: typeof getDirectories
    Partial<CompilerHost>
    transpileOnly

    是否仅转换代码但不进行类型检查,启用后将可以大幅提升编译性能

    boolean
    renameModuleName

    自定义重命名模块名的逻辑

    (moduleName: string, sourceFile: SourceFile, program: Program) => string
    getCustomTransformers

    获取自定义转换器

    (program: Program, compiler: TypeScriptCompiler) => CustomTransformers
    plugins

    自定义转换器插件

    展开子属性
    • factory: (program: Program, compiler: (Circular)TypeScriptCompilerPlugin) => CustomTransformers | (TransformerFactory<SourceFile> | CustomTransformerFactory)[] — 返回转换器的工厂函数
    • transform: string — 插件模块名
    • import: string — 从插件模块中导入的名称
    • type: "compilerOptions" | "program" | "raw" | "config" | "checker" — 从插件模块中导入的类型
    • after: boolean — 是否应该在内置 JS 转换器之后执行转换器
    • afterDeclarations: boolean — 是否应该在生成 .d.ts 文件之后执行转换器
    TypeScriptCompilerPlugin[]

    查看源码
    (共 16 行)
    TypeScriptCompilerPlugin 接口

    表示一个 TypeScript 插件

    属性

    属性名说明类型
    [option: string]any
    factory

    返回转换器的工厂函数

    (program: Program, compiler: TypeScriptCompilerPlugin) => CustomTransformers | (TransformerFactory<SourceFile> | CustomTransformerFactory)[]
    transform

    插件模块名

    string
    import

    从插件模块中导入的名称

    string
    type

    从插件模块中导入的类型

    "compilerOptions" | "program" | "raw" | "config" | "checker"
    after

    是否应该在内置 JS 转换器之后执行转换器

    boolean
    afterDeclarations

    是否应该在生成 .d.ts 文件之后执行转换器

    boolean

    查看源码
    (共 15 行)
    TypeScriptCompileResult 接口

    表示 TypeScript 编译结果

    属性

    属性名说明类型
    content

    生成的 JavaScript 代码

    string
    sourceFile

    当前文件的源文件对象

    SourceFile
    program

    本次使用的编译对象

    Program
    sourceMap(可选)

    生成的源映射的内容

    string
    declaration(可选)

    生成的声明文件内容

    string
    errors(可选)

    生成时累积的错误

    展开子属性
    • reportsUnnecessary: {} — May store more in future. For now, this will simply be true to indicate when a diagnostic is an unused-identifier diagnostic.
    • reportsDeprecated: {}
    • source: string
    • relatedInformation: array
      • category: DiagnosticCategory
      • code: number
      • file: SourceFile
      • start: number
      • length: number
      • messageText
        • messageText: string
        • category: DiagnosticCategory
        • code: number
        • next: (Circular)DiagnosticMessageChain[]
    • category: DiagnosticCategory
    • code: number
    • file: SourceFile
    • start: number
    • length: number
    • messageText
      • messageText: string
      • category: DiagnosticCategory
      • code: number
      • next: (Circular)DiagnosticMessageChain[]
    Diagnostic[]

    查看源码
    (共 25 行)
    readTSConfig

    读取指定的 tsconfig.json,如果配置文件不存在则返回 undefined

    参数
    参数名说明类型
    path

    要读取的配置

    string
    (可选)configFilePaths

    如果传递了空集合,则返回所有已读取的文件路径

    Set<string>
    返回值
    类型:CompilerOptions

    查看源码
    (共 40 行)
    normalizeCompilerOptions

    规范化 TypeScript 编译器选项对象

    参数
    参数名说明类型
    options

    用户设置的选项

    展开子属性
    • allowJs: boolean
    • allowSyntheticDefaultImports: boolean
    • allowUmdGlobalAccess: boolean
    • allowUnreachableCode: boolean
    • allowUnusedLabels: boolean
    • alwaysStrict: boolean
    • baseUrl: string
    • bundledPackageName: string
    • charset: string
    • checkJs: boolean
    • declaration: boolean
    • declarationMap: boolean
    • emitDeclarationOnly: boolean
    • declarationDir: string
    • disableSizeLimit: boolean
    • disableSourceOfProjectReferenceRedirect: boolean
    • disableSolutionSearching: boolean
    • disableReferencedProjectLoad: boolean
    • downlevelIteration: boolean
    • emitBOM: boolean
    • emitDecoratorMetadata: boolean
    • experimentalDecorators: boolean
    • forceConsistentCasingInFileNames: boolean
    • importHelpers: boolean
    • importsNotUsedAsValues: ImportsNotUsedAsValues
    • inlineSourceMap: boolean
    • inlineSources: boolean
    • isolatedModules: boolean
    • jsx: JsxEmit.None | JsxEmit.Preserve | JsxEmit.React | ... 5 more ...JsxEmit.ReactNative | JsxEmit.ReactJSX | JsxEmit.ReactJSXDev | "none" | "preserve" | "react" | "react-jsx"
    • keyofStringsOnly: boolean
    • lib: string[]
    • locale: string
    • mapRoot: string
    • maxNodeModuleJsDepth: number
    • module: ModuleKind.None | ModuleKind.CommonJS | ModuleKind.AMD | ... 10 more ...ModuleKind.UMD | ModuleKind.System | ModuleKind.ES2015 | ModuleKind.ES2020 | ModuleKind.ESNext | "es6" | "es2015" | "esnext" | "none" | "commonjs" | "system" | "umd"
    • moduleResolution: ModuleResolutionKind.Classic | ModuleResolutionKind.NodeJs | "node" | "classic"
    • newLine: NewLineKind.CarriageReturnLineFeed | NewLineKind.LineFeed | "CRLF" | "LF"
    • noEmit: boolean
    • noEmitHelpers: boolean
    • noEmitOnError: boolean
    • noErrorTruncation: boolean
    • noFallthroughCasesInSwitch: boolean
    • noImplicitAny: boolean
    • noImplicitReturns: boolean
    • noImplicitThis: boolean
    • noStrictGenericChecks: boolean
    • noUnusedLocals: boolean
    • noUnusedParameters: boolean
    • noImplicitUseStrict: boolean
    • noPropertyAccessFromIndexSignature: boolean
    • assumeChangesOnlyAffectDirectDependencies: boolean
    • noLib: boolean
    • noResolve: boolean
    • noUncheckedIndexedAccess: boolean
    • out: string
    • outDir: string
    • outFile: string
    • paths: MapLike<string[]>
    • preserveConstEnums: boolean
    • preserveSymlinks: boolean
    • project: string
    • reactNamespace: string
    • jsxFactory: string
    • jsxFragmentFactory: string
    • jsxImportSource: string
    • composite: boolean
    • incremental: boolean
    • tsBuildInfoFile: string
    • removeComments: boolean
    • rootDir: string
    • rootDirs: string[]
    • skipLibCheck: boolean
    • skipDefaultLibCheck: boolean
    • sourceMap: boolean
    • sourceRoot: string
    • strict: boolean
    • strictFunctionTypes: boolean
    • strictBindCallApply: boolean
    • strictNullChecks: boolean
    • strictPropertyInitialization: boolean
    • stripInternal: boolean
    • suppressExcessPropertyErrors: boolean
    • suppressImplicitAnyIndexErrors: boolean
    • target: string | ScriptTarget.ES3 | ScriptTarget.ES5 | ... 5 more ...ScriptTarget.ES2015 | ScriptTarget.ES2016 | ScriptTarget.ES2017 | ScriptTarget.ES2018 | ScriptTarget.ES2019 | ScriptTarget.ESNext | ScriptTarget.JSON
    • traceResolution: boolean
    • resolveJsonModule: boolean
    • types: string[]
    • typeRoots: string[] — Paths used to compute primary types search locations
    • esModuleInterop: boolean
    • useDefineForClassFields: boolean
    Merge<CompilerOptions, {target?: stringmodule?: "es6" | "es2015" | "esnext" | "none" | "commonjs" | "amd" | "system" | "umd"jsx?: "none" | "preserve" | "react-native" | "react" | "react-jsx"moduleResolution?: "node" | "classic"newLine?: "CRLF" | "LF"}>
    baseDir

    解析相对地址的基路径

    string
    返回值
    类型:CompilerOptions
    另参考