How do you stop tsc from processing a Javascript file required by another?
I want to run full checks on my main index.js, but it requires() a generated.js Javascript file created by emcc, which is perfectly fine, but doesn't pass a lot of tsc's checks.
I tried adding the file to my exclude list in my tsconfig.json, like:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"lib": [
"dom",
"webworker"
],
"allowJs": true,
"checkJs": true,
"outDir": "./dist",
"noImplicitAny": false,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": [
"index.js"
],
"exclude": [
"generated.js"
]
}
but that had no effect. When I run tsc --build tsconfig.json I get a tone of errors from generated.js.
The tsconfig.json file looks for typescript files by default, while you are providing it .js files. you can change the file extensions from .js to .ts to let typescript look after them. so your tsconfig.json file may end up looking like this:
{
"compilerOptions": {
"target": "es5",
"module": "system",
"lib": [
"dom",
"webworker"
],
"allowJs": true,
"checkJs": true,
"outDir": "./dist",
"noImplicitAny": false,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": [
"index.ts"
],
"exclude": [
"node_modules",
"generated.ts"
]
}
Related
I am trying to create a simple node.js cli tool called convertxml. I have defined the bin field in my package.json and ran npm link.
When I run it, I am getting the following error when I run convertxml in powershell/cmd.
using the following package.json:
with the following tsconfig:
{
"compilerOptions": {
"incremental": true,
"target": "ES5",
"lib": [
"DOM",
"DOM.Iterable",
"ESNext"
],
"jsx": "preserve",
"module": "ES6",
"moduleResolution": "node",
"resolveJsonModule": true,
"noEmit": true,
"isolatedModules": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx"
],
"exclude": [
"node_modules",
"**/*.js"
]
}
The weird thing is that when I run node ./converter.js it runs perfectly.
Anyone knows how to solve this?
I need to import a javascript lib into my project without .d.ts files (for some reason it doesn't have), while sadly I cannot use 'import' or 'require' at the same time (the ts poject structure is old but cannot upgrade).Is there still any solution?The tsconfig.json may like the following:
{
"compilerOptions": {
"target": "ES5",
"module": "amd",
"skipLibCheck": true,
"lib": [
"DOM",
"ES2015"
],
"allowJs": true,
"checkJs": false,
"outFile": "./out/js/bundle.js"
},
"exclude": [
"node_modules"
],
"include": [
"./lib/lib.js",
"./src/**/*.ts"
]
}
This is my tsconfig.json file
{
"compilerOptions": {
"watch": false,
"target": "ES2020",
"allowJs": true,
"module": "ES2020",
"resolveJsonModule": true,
"lib": [
"ES2020"
],
"esModuleInterop": true,
"moduleResolution": "Node",
"outDir": "./dist/",
"sourceMap": false,
},
"include": [
"src/**/*.*",
"src/json/.env"
]
}
Basically I want that the js files end up in dist/index.js and not in dist/src/index.js
This is how it ends up
This is how I want it to be
What should I change and why?
Update tsconfig.json as below
{
"compilerOptions": {
"watch": false,
"target": "ES2020",
"allowJs": true,
"module": "ES2020",
"resolveJsonModule": true,
"lib": [
"ES2020"
],
"esModuleInterop": true,
"moduleResolution": "Node",
"outDir": "./dist/",
"sourceMap": false,
"rootDir": "./src", /** Specifies the root directory of input files */
}
}
The compiler looks at your tsconfig.json and finds all the files it needs to build by looking at "files"/"includes"/"excludes" then following all ///<reference .. /> and import statements.. now it has a transitive closure of all files it needs to build. you can use tsc --listFiles to see the list.
The next step is generating the output. for every input file (i.e. a .ts/.tsx file) it needs to generate an matching output (a .js.jsx file). To figure out the file path of the generated output file it will chop off the "rootDir" from the input, then prepend the "outDir" to it.
https://github.com/Microsoft/TypeScript/issues/25646
I can't fix next error: when I build project for production, building is successfull. But when I build project with param --prod it's fail.
Error like this:
in node_modules\ng-zorro-antd\antd.d.ts.ɵep.html(1,999): : Object is possibly 'null'.
My TSlint setup is:
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"module": "es2015",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"strict": true,
"target": "es5",
"typeRoots": [
"node_modules/#types"
],
"lib": [
"es2017",
"dom"
],
"removeComments": true
},
"exclude": [
"node_modules",
"**/*.spec.ts",
"**/*.ts.ɵin.html"
]
}
I tried to add node_modules folder to "exclude" but it doesn't work.
What I have:
./src/myfile.ts
./test/mytest.spec.ts
And tsc should create a javascript (myfile.js) and definition file (myfile.d.ts) in the ./build location.
My tsconfig.ts:
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noImplicitAny": false,
"rootDir": ".",
"outDir": "./build",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"declaration": true
},
"files": [
"./src/index.ts"
],
"exclude": [
".vscode",
".git",
"build",
"node_modules",
"test"
],
"compileOnSave": false,
"buildOnSave": false,
"atom": {
"rewriteTsconfig": false
}
}
But this config file generates the myfile.js in the ./build/src/ sub-folder.
Note that I cannot use "rootDir": "./src" because then the test folder is not compiled (which gives problems when running tests with karma + webpack?)
And tsc should create a javascript (myfile.js) and definition file (myfile.d.ts) in the ./build location.
TypeScript is not nicely modeled to handle bundling. That is something you should use some other tool to do
More
e.g. Webpack quickstart: https://basarat.gitbooks.io/typescript/content/docs/quick/browser.html
Don't recommend out / outFile : https://basarat.gitbooks.io/typescript/content/docs/tips/outFile.html