I have a .d.ts file, and would like to compile it down to a .js file. Is this possible using the tsc command?
I know that the tsc command can take any .ts file and then compile it down .js file (I have tested this). However, I couldn't get it to take a .d.ts file as an input since it did not output any .js file for me.
I have tried to play around with tsconfig.json and this how my file looks like at the moment:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"sourceMap": true,
"outDir": "ali_output_javascript"
}
}
I also know the following about .d.ts and .ts files. Typescript declaration files (.d.ts) describe the shape of a JavaScript file. It allows you to use existing JavaScript code in TypeScript without having to write all the code in TypeScript. Typescript files (.ts) is just the standard file extension when writing in typescript.
From this medium article (https://medium.com/jspoint/typescript-compilation-the-typescript-compiler-4cb15f7244bc), it does say that TypeScript compilation looks for file extensions .ts and .d.ts:
Normally, you would use files or include option and provide file paths or glob patterns to include TypeScript files in the compilation process. However, you can drop the .ts extension from the glob pattern and let the TypeScript compiler search for the input files.
When a glob pattern doesn’t have a file extension, TypeScript looks for the files ending with .ts or .d.ts extension in the directory pointed by the glob pattern. The .d.ts file is a TypeScript declaration file, we have discussed this file type in the Declaration Files lesson (coming soon). When the allowJS compiler-option is set to true, the TypeScript will also search for .js files as well.
Please let me know if you any suggestions, and thank you for your time.
You misunderstood what .d.ts files are.
They are not supposed to be transpiled into .js files, .ts files are supposed to be transpiled into .js files and .d.ts files for strong typing in a JavaScript IDE, or for further use into a TypeScript project.
Also, .d.ts are not executable and cannot produce executable files as they are just empty shells with types, which is also know as ambient declaration, in TypeScript terms, which means "without implementation".
Related
I have some files
Compenent.js
index.js
types.js
How i can avoid Type annotations can only be used in TypeScript files. error, and show types in js files?
I had a file, module.ts written in TypeScript and compiled it to JavaScript for production deployment. What was I left with were the following files;
module.js (the TypeScript code compiled into JavaScript)
module.d.ts (the declaration file)
module.js.map (the source file)
Now I want to revert the module.js back to module.ts file using all these 3 files because I don't have that .ts file anymore.
I tried loading module.js inside the browser, hoping it should load it's .ts file into the Developer Tools, using the Sourcemap, but it doesn't. I believe I am missing the point here of having a Sourcemap after all.
I have a project, it's not Angular2 or anything. I'm just using typescript in vscode.
I've setup my tsconfig.json to output my .ts files to a single file in ./build. However, for convenience purposes I would also like to include a folder or file i.e: /lib which contains all the js files I'm using such as linq (http://www.javascriptlinq.com/), jquery & bootstrap etc...
Basically, I want the end product of my project concists of 1 or 2 .js files.
Thanks to toskv in the rely, the workaround for me was to add
"allowJs": true
in tsconfig.json. Also,
"include": [ "src/**/*" ]
doesn't like it when i add folders deeper than that so i had to chuck them in
/src/lib
rather than
/src/lib/somelib/file.js
I just downaloade TypeScript for Visual Studio 2015. I am writing .ts file and it created .js file in the same level in solution. Does anyone know how to hide the .js file or have it 'inside' .ts so I could just expand .ts tree and see .js?
You can configure your project to create .js files in a specific folder.
In VS 2015, you can set the build directory from "Redirect Javascript output to directory" in the typescript build section of your project properties page.
If you use a tsconfig.json the project property pages will all be grayed out but you can specify the directory in the tsconfig.json file:
"compilerOptions": {
"outDir": "./built",
In the image below on the left in the folders you can see my /src Typescript (blue) compiled into my /dist (purple) Javascript using tsc.
You can see in the source file on the left that references a .ts module file that it isn't compiled into referencing a .js module file on the right.
Why not? How could the Javascript possibly run if tsc doesn't convert references?
Second question: I then tried manually changing the compiled reference from .ts to .js and running node dist/server.js but I get the error cannot find module tools/typescriptImport.js'. Why does node fail to find the module when it is correctly referenced (and you can see on the far right that it is a module)?
For starters, you have to remove the .ts extension from the import. TypeScript says that it treats it as a static string and won't change it.
Second, out of experience, I guess using a .d.ts file may solve your module not found error. I have solved many times by using this small hack. You can reference it using /// <reference path="tools/typeScriptImports.d.ts" />. Imagine .d.ts as the header file for TypeScript.
Lastly, try and make the path relative to the server.js file. So: ./tools/typeScriptImports.
You are not supposed to write extension .ts in import commands.
Corresponding documentation: http://www.typescriptlang.org/Handbook#modules-going-external