Tsconfig and Declaration Files
tsconfig
Our TypeScript Compiler offers many options for configuration and customization to fit your specific use case.
Let's navigate to our ts_sandbox and see what it can do:
$ tsc --init
message TS6071: Successfully created a tsconfig.json file.Oof!--that's a bit overwhelming when we are first getting started. More information on these options are available in the docs here.
For our codelalong today, let's use this pared down snippet.
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es6",
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "build",
"baseUrl": ".",
"paths": {
"*": [
"node_modules/*"
]
}
},
"include": [
"src/**/*"
]
}Declaration Files and @types from npm
.d.ts
TypeScript allows us to declare the shape of our types, variables and functions in separate files (for globals and/or modules). By using, the extension .d.ts the TypeScript Compiler/Linter can use these declarations in your project.
If you happen to be writing a library or working on a large project, this might be a good option. Today we will declare our types inline and at the head of our file.
More information on Declaration Files
@types
When using third-party libraries like Express, Sequelize or Mongoose it can be annoying and frustrating trying to determine what our Types should be.
Lucky for us the fine folks at Definitely Typed have created a repo of type declarations that can be installed via npm for most of the libraries that are widely used.
Once installed, we can basically write normal javascript while implementing the Type guards of TS... making the example above look like this:
Last updated
Was this helpful?