sudo sudo npm install -g typescript
readonly keyword interface TQ84 {
id : number;
text: string;
};
let tq84 : TQ84 = {
id : 42,
text: 'hello world'
};
class TQ84 {
id : number;
text: string;
constructor(id: number, text: string) {
this.id = id;
this.text = text;
}
};
let tq84 : TQ84 = new TQ84(42, 'Hello world');
console.log(tq84.id);
enum: type phase =
'initialized' |
'in-progress' |
'waiting' |
'closed';
let prjPhase : phase = 'in-progress';
function ident<T>(val: T): T {
return val;
}
@xyz indicates a decorator. xyz is a function which will be invoked with the class or class member that follows @xyz. experimentalDecorators must be enabled (tsc --experimentalDecorators src-file.ts or in the tsconfig.json file). const deco = (cls: new() => CLS_1): void => {
console.log(cls);
}
class BASE {}
@deco
class CLS_1 extends BASE {}
tsc. tsc is written in TypeScript. Thus, it can be transpiled into JavaScript and executed in a web browser. tsc options are descriped at aka.ms/tsc tsc --all… --all | Show all compiler options. | |
-b | --build | Build one or more projects and their dependencies, if out of date. (See also #tsc-build-options[buld options]). |
-h | --help | Print this message. |
-? | --help | |
--init | Initializes a TypeScript project and creates a tsconfig.json file. | |
--listFilesOnly | Print names of files that are part of the compilation and then stop processing. | |
--locale | Set the language of the messaging from TypeScript. This does not affect emit. | |
-p | --project | Compile the project given the path to its configuration file, or to a folder with a tsconfig.json. |
--showConfig | Print the final configuration instead of building. | |
-v | --version | Print the compiler's version. |
-w | --watch | Watch input files. See also watch options |
| type | default | ||
--allowJs | Allow JavaScript files to be a part of your program. Use the checkJS option to get errors from these files. | boolean | false |
--checkJs | Enable error reporting in type-checked JavaScript files. | boolean | false |
--maxNodeModuleJsDepth | Specify the maximum folder depth used for checking JavaScript files from node_modules. Only applicable with allowJs. | number | 0 |
| type | default | ||
--allowSyntheticDefaultImports | Allow import x from y when a module doesn't have a default export. | boolean | module === "system" or esModuleInterop |
--esModuleInterop | Emit additional JavaScript to ease support for importing CommonJS modules. This enables allowSyntheticDefaultImports for type compatibility. | boolean | false |
--forceConsistentCasingInFileNames | Ensure that casing is correct in imports. | boolean | false |
--isolatedModules | Ensure that each file can be safely transpiled without relying on other imports. | boolean | false |
--preserveSymlinks | Disable resolving symlinks to their realpath. This correlates to the same flag in node. | boolean | false |
| type | default | |||
--allowUmdGlobalAccess | Allow accessing UMD globals from modules. | boolean | false | |
--baseUrl | Specify the base directory to resolve non-relative module names. | |||
-m | --module | Specify what module code is generated. | none, commonjs, amd, umd, system, es6/es2015, es2020, es2022, esnext, node16 or nodenext | undefined |
--moduleResolution | Specify how TypeScript looks up a file from a given module specifier. | classic, node, node16 or nodenext | module === AMD or UMD or System or ES6, then Classic, Otherwise Node | |
--moduleSuffixes | List of file name suffixes to search when resolving a module. | |||
--noResolve | Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. | boolean | false | |
--paths | Specify a set of entries that re-map imports to additional lookup locations. | undefined | ||
--resolveJsonModule | Enable importing .json files. | boolean | false | |
--rootDir | Specify the root folder within your source files. | string | Computed from the list of input files | |
--rootDirs | Allow multiple folders to be treated as one when resolving modules. | one or more strings | Computed from the list of input files | |
--typeRoots | Specify multiple folders that act like ./node_modules/@types. | |||
--types | Specify type package names to be included without being referenced in a source file. |
| type | default | ||
--allowUnreachableCode | Disable error reporting for unreachable code. | boolean | undefined |
--allowUnusedLabels | Disable error reporting for unused labels. | boolean | undefined |
--alwaysStrict | Ensure use strict is always emitted. | boolean | false, unless strict is set |
--exactOptionalPropertyTypes | Interpret optional property types as written, rather than adding undefined. | boolean | false |
--noFallthroughCasesInSwitch | Enable error reporting for fallthrough cases in switch statements. | boolean | false |
--noImplicitAny | Enable error reporting for expressions and declarations with an implied any type. | boolean | false, unless strict is set |
--noImplicitOverride | Ensure overriding members in derived classes are marked with an override modifier. | boolean | false |
--noImplicitReturns | Enable error reporting for codepaths that do not explicitly return in a function. | boolean | false |
--noImplicitThis | Enable error reporting when this is given the type any. | boolean | false, unless strict is set |
--noPropertyAccessFromIndexSignature | Enforces using indexed accessors for keys declared using an indexed type. | boolean | false |
--noUncheckedIndexedAccess | Add undefined to a type when accessed using an index. | boolean | false |
--noUnusedLocals | Enable error reporting when local variables aren't read. | boolean | false |
--noUnusedParameters | Raise an error when a function parameter isn't read. | boolean | false |
--strict | Enable all strict type-checking options. | boolean | false |
--strictBindCallApply | Check that the arguments for bind, call, and apply methods match the original function. | boolean | false, unless strict is set |
--strictFunctionTypes | When assigning functions, check to ensure parameters and the return values are subtype-compatible. | boolean | false, unless strict is set |
--strictNullChecks | When type checking, take into account null and undefined. | boolean | false, unless strict is set |
--strictPropertyInitialization | Check for class properties that are declared but not set in the constructor. | boolean | false, unless strict is set |
--useUnknownInCatchVariables | Default catch clause variables as unknown instead of any. | boolean | false |
| type | default | ||
--assumeChangesOnlyAffectDirectDependencies | Have recompiles in projects that use incremental and watch mode assume that changes within a file will only affect files directly depending on it. | boolean | false |
| type | default | ||
--charset | No longer supported. In early versions, manually set the text encoding for reading files. | string | utf8 |
--keyofStringsOnly | Make keyof only return strings instead of string, numbers or symbols. Legacy option. | boolean | false |
--noImplicitUseStrict | Disable adding use strict directives in emitted JavaScript files. | boolean | false |
--noStrictGenericChecks | Disable strict checking of generic signatures in function types. | boolean | false |
--out | Deprecated setting. Use outFile instead. | ||
--suppressExcessPropertyErrors | Disable reporting of excess property errors during the creation of object literals. | boolean | false |
--suppressImplicitAnyIndexErrors | Suppress noImplicitAny errors when indexing objects that lack index signatures. | boolean | false |
--composite | Enable constraints that allow a TypeScript project to be used with project references. | boolean | false | |
--disableReferencedProjectLoad | Reduce the number of projects loaded automatically by TypeScript. | boolean | false | |
--disableSolutionSearching | Opt a project out of multi-project reference checking when editing. | boolean | false | |
--disableSourceOfProjectReferenceRedirect | Disable preferring source files instead of declaration files when referencing composite projects. | boolean | false | |
-i | --incremental | Save .tsbuildinfo files to allow for incremental compilation of projects. | boolean | false, unless composite is set |
--tsBuildInfoFile | Specify the path to .tsbuildinfo incremental compilation file. | string | .tsbuildinfo |
| type | default | ||
--declaration, -d | Generate .d.ts files from TypeScript and JavaScript files in your project. | boolean | false, unless composite is set |
--declarationDir | Specify the output directory for generated declaration files. | ||
--declarationMap | Create sourcemaps for d.ts files. | boolean | false |
--downlevelIteration | Emit more compliant, but verbose and less performant JavaScript for iteration. | boolean | false |
--emitBOM | Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. | boolean | false |
--emitDeclarationOnly | Only output d.ts files and not JavaScript files. | boolean | false |
--importHelpers | Allow importing helper functions from tslib once per project, instead of including them per-file. | boolean | false |
--importsNotUsedAsValues | Specify emit/checking behavior for imports that are only used for types. | remove, preserve or error | remove |
--inlineSourceMap | Include sourcemap files inside the emitted JavaScript. | boolean | false |
--inlineSources | Include source code in the sourcemaps inside the emitted JavaScript. | boolean | false |
--mapRoot | Specify the location where debugger should locate map files instead of generated locations. | ||
--newLine | Set the newline character(s) for emitting files. | crlf or lf | Platform specific |
--noEmit | Disable emitting files from a compilation. | boolean | false |
--noEmitHelpers | Disable generating custom helper functions like __extends in compiled output. | boolean | false |
--noEmitOnError | Disable emitting files if any type checking errors are reported. | boolean | false |
--outDir | Specify an output folder for all emitted files. | ||
--outFile | Specify a file that bundles all outputs into one JavaScript file. If declaration is true, also designates a file that bundles all .d.ts output. | ||
--preserveConstEnums | Disable erasing const enum declarations in generated code. | boolean | false |
--preserveValueImports | Preserve unused imported values in the JavaScript output that would otherwise be removed. | boolean | false |
--removeComments | Disable emitting comments. | boolean | false |
--sourceMap | Create source map files for emitted JavaScript files. | boolean | false |
--sourceRoot | Specify the root path for debuggers to find the reference source code. | ||
--stripInternal | Disable emitting declarations that have @internal in their JSDoc comments. | boolean | false |
| type | default | ||
--diagnostics | Output compiler performance information after building. | boolean | false |
--explainFiles | Print files read during the compilation including why it was included. | boolean | false |
--extendedDiagnostics | Output more detailed compiler performance information after building. | boolean | false |
--generateCpuProfile | Emit a v8 CPU profile of the compiler run for debugging. | string | profile.cpuprofile |
--generateTrace | Generates an event trace and a list of types. | ||
--listEmittedFiles | Print the names of emitted files after a compilation. | boolean | false |
--listFiles | Print all of the files read during the compilation. | boolean | false |
--traceResolution | Log paths used during the moduleResolution process. | boolean | false |
| type | default | ||
--disableSizeLimit | Remove the 20mb cap on total source code size for JavaScript files in the TypeScript language server. | boolean | false |
--plugins | Specify a list of language service plugins to include. | one or more | undefined |
| type | default | |||
--emitDecoratorMetadata | Emit design-type metadata for decorated declarations in source files. | boolean | false | |
--experimentalDecorators | Enable experimental support for TC39 stage 2 draft decorators. | boolean | false | |
--jsx | Specify what JSX code is generated. | preserve, react, react-native, react-jsx or react-jsxdev | undefined | |
--jsxFactory | Specify the JSX factory function used when targeting React JSX emit, e.g. React.createElement or h. | string | React.createElement | |
--jsxFragmentFactory | Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. React.Fragment or Fragment. | string | React.Fragment | |
--jsxImportSource | Specify module specifier used to import the JSX factory functions when using jsx: react-jsx*. | string | react | |
--lib | Specify a set of bundled library declaration files that describe the target runtime environment. | one or more of es5, es6/es2015, es7/es2016, es2017, es2018, es2019, es2020, es2021, es2022, esnext, dom, dom.iterable, webworker, webworker.importscripts, webworker.iterable, scripthost, es2015.core, es2015.collection, es2015.generator, es2015.iterable, es2015.promise, es2015.proxy, es2015.reflect, es2015.symbol, es2015.symbol.wellknown, es2016.array.include, es2017.object, es2017.sharedmemory, es2017.string, es2017.intl, es2017.typedarrays, es2018.asyncgenerator, es2018.asynciterable/esnext.asynciterable, es2018.intl, es2018.promise, es2018.regexp, es2019.array, es2019.object, es2019.string, es2019.symbol/esnext.symbol, es2019.intl, es2020.bigint/esnext.bigint, es2020.date, es2020.promise, es2020.sharedmemory, es2020.string, es2020.symbol.wellknown, es2020.intl, es2020.number, es2021.promise/esnext.promise, es2021.string, es2021.weakref/esnext.weakref, es2021.intl, es2022.array/esnext.array, es2022.error, es2022.intl, es2022.object, es2022.sharedmemory, es2022.string/esnext.string, esnext.intl | undefined | |
--moduleDetection | Control what method is used to detect module-format JS files. | legacy, auto, force | auto: Treat files with imports, exports, import.meta, jsx (with jsx: react-jsx), or esm format (with module: node16+) as modules. | |
--noLib | Disable including any library files, including the default lib.d.ts. | boolean | false | |
--reactNamespace | Specify the object invoked for createElement. This only applies when targeting react JSX emit. | string | React | |
-t | --target | Set the JavaScript language version for emitted JavaScript and include compatible library declarations. | es3, es5, es6/es2015, es2016, es2017, es2018, es2019, es2020, es2021, es2022, esnext | es3 |
--useDefineForClassFields | Emit ECMAScript-standard-compliant class fields. | boolean | true for ES2022 and above, including ESNext. |
| type | default | ||
--noErrorTruncation | Disable truncating types in error messages. | boolean | false |
--preserveWatchOutput | Disable wiping the console in watch mode. | boolean | false |
--pretty | Enable color and formatting in TypeScript's output to make compiler errors easier to read. | boolean | true |
| type | default | ||
--skipDefaultLibCheck | Skip type checking .d.ts files that are included with TypeScript. | boolean | false |
--skipLibCheck | Skip type checking all .d.ts files. | boolean |
--watch (-w) options will start watching the current project for the file changes. --watch: | type | default | ||
--watchFile | Specify how the TypeScript watch mode works. | fixedpollinginterval, prioritypollinginterval, dynamicprioritypolling, fixedchunksizepolling, usefsevents or usefseventsonparentdirectory | usefsevents |
--watchDirectory | Specify how directories are watched on systems that lack recursive file-watching functionality. | usefsevents, fixedpollinginterval, dynamicprioritypolling or fixedchunksizepolling | usefsevents |
--fallbackPolling | Specify what approach the watcher should use if the system runs out of native file watchers. | fixedinterval, priorityinterval, dynamicpriority or fixedchunksize | priorityinterval |
--synchronousWatchDirectory | Synchronously call callbacks and update the state of directory watchers on platforms that don`t support recursive watching natively. | boolean | false |
--excludeDirectories | Remove a list of directories from the watch process. | ||
--excludeFiles | Remove a list of files from the watch mode's processing. |
--build (-b) will make tsc behave more like a build orchestrator than a compiler. -v | --verbose | Enable verbose logging. |
-d | --dry | Show what would be built (or deleted, if specified with --clean) |
-f | --force | Build all projects, including those that appear to be up to date. |
--clean | Delete the outputs of all projects. |
tsconfig.json. tsconfig.json file specifies the compiler options and the root files to compile this project. tsconfig.json. jsoncfig.json file. tsc requires the compiled file to have one of the supported extensions: .ts | An implementation file. It contains executable code to be transpiled to JavaScript. |
.tsx | |
.d.ts | A declaration file*. It contains type information only. |
.cts | |
.d.cts | |
.mts | |
.d.mts |