我想在Typescript中编写代码,从运行时对象生成tsconfig.json
文件。我在哪里可以获得此对象的定义,例如如下所示:
interface TSConfig [
compileOnSave?: boolean;
files?: string[];
}
答案 0 :(得分:1)
你可以重用typescript模块中定义的一些类型(你可以通过npm install typescript
得到它们)来获取编译器选项和类型获取选项,尽管没有为完整的tsconfig.json定义类型,我们需要做一些条件类型魔术(在typescript 2.8中可用)来获取编译器选项的类型。枚举将被导出,因此您可以直接使用它们。
import * as ts from 'typescript' // Import will be elided as long as we only use types from it, so we don't have the compiler code loaded at runtime
type CompilerOptions = typeof ts.parseCommandLine extends (...args: any[])=> infer TResult ?
TResult extends { options: infer TOptions } ? TOptions : never : never;
type TypeAcquisition = typeof ts.parseCommandLine extends (...args: any[])=> infer TResult ?
TResult extends { typeAcquisition?: infer TTypeAcquisition } ? TTypeAcquisition : never : never;
interface TsConfig {
compilerOptions: CompilerOptions;
exclude: string[];
compileOnSave: boolean;
extends: string;
files: string[];
include: string[];
typeAcquisition: TypeAcquisition
}
注意这样做的好处是,当您更新软件包时,编译器团队对CompilerOptions
和TypeAcquisition
类型所做的更改将反映在您的代码中。缺点是它从编译器API中提取了一些类型,这些类型可能由于某种原因没有直接暴露(尽管如果编译器团队会更改它们,它们是ts.parseCommandLine
结果的一部分,那么它将是一个破坏API变化)
答案 1 :(得分:0)
在源代码库中,您可以查找CompilerOptions https://github.com/Microsoft/TypeScript/blob/master/lib/typescript.d.ts#L2305,TypeAcquisition https://github.com/Microsoft/TypeScript/blob/master/lib/typescript.d.ts#L2379的一些类型以及解析tsconfig.json https://github.com/Microsoft/TypeScript/blob/master/lib/typescript.d.ts#L2434后使用的接口
在json.schemastore上存在tsconfig.json的架构:http://json.schemastore.org/tsconfig
在官方网站上有tsconfig:https://www.typescriptlang.org/docs/handbook/tsconfig-json.html
的说明基于他们我创造了这个:
interface MapLike<T> {
[index: string]: T;
}
enum ModuleResolutionKind {
Classic = 1,
NodeJs = 2,
}
interface CompilerOptions {
allowJs?: boolean;
allowSyntheticDefaultImports?: boolean;
allowUnreachableCode?: boolean;
allowUnusedLabels?: boolean;
alwaysStrict?: boolean;
baseUrl?: string;
charset?: string;
checkJs?: boolean;
declaration?: boolean;
emitDeclarationOnly?: boolean;
declarationDir?: string;
disableSizeLimit?: boolean;
downlevelIteration?: boolean;
emitBOM?: boolean;
emitDecoratorMetadata?: boolean;
experimentalDecorators?: boolean;
forceConsistentCasingInFileNames?: boolean;
importHelpers?: boolean;
inlineSourceMap?: boolean;
inlineSources?: boolean;
isolatedModules?: boolean;
jsx?: JsxEmit;
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;
noLib?: boolean;
noResolve?: boolean;
out?: string;
outDir?: string;
outFile?: string;
paths?: MapLike<string[]>;
preserveConstEnums?: boolean;
preserveSymlinks?: boolean;
project?: string;
reactNamespace?: string;
jsxFactory?: string;
removeComments?: boolean;
rootDir?: string;
rootDirs?: string[];
skipLibCheck?: boolean;
skipDefaultLibCheck?: boolean;
sourceMap?: boolean;
sourceRoot?: string;
strict?: boolean;
strictFunctionTypes?: boolean;
strictNullChecks?: boolean;
strictPropertyInitialization?: boolean;
suppressExcessPropertyErrors?: boolean;
suppressImplicitAnyIndexErrors?: boolean;
target?: ScriptTarget;
traceResolution?: boolean;
types?: string[];
/** Paths used to compute primary types search locations */
typeRoots?: string[];
esModuleInterop?: boolean;
}
interface TypeAcquisition {
enableAutoDiscovery?: boolean;
enable?: boolean;
include?: string[];
exclude?: string[];
[option: string]: string[] | boolean | undefined;
}
enum ModuleKind {
None = 0,
CommonJS = 1,
AMD = 2,
UMD = 3,
System = 4,
ES2015 = 5,
ESNext = 6,
}
enum JsxEmit {
None = 0,
Preserve = 1,
React = 2,
ReactNative = 3,
}
enum NewLineKind {
CarriageReturnLineFeed = 0,
LineFeed = 1,
}
enum ScriptTarget {
ES3 = 0,
ES5 = 1,
ES2015 = 2,
ES2016 = 3,
ES2017 = 4,
ES2018 = 5,
ESNext = 6,
Latest = 6,
}
interface TsConfig {
compilerOptions: CompilerOptions;
exclude: string[];
compileOnSave: boolean;
extends: string;
files: string[];
include: string[];
typeAcquisition: TypeAcquisition
}