tsc

Typescript compiler (tsc) and relative path

时光毁灭记忆、已成空白 提交于 2019-12-11 06:01:57
问题 I want to compile the file and its dependencies. My goal import dependencies using the relative path. example in /root/subfolder/myFile.ts : import { myFunction } from '~/functions' tilde ~ should show that myFunction is from the root folder root |-- functions.ts `-- subfolder `-- myFile.ts 回答1: You can add the following to compilerOptions in your tsconfig.json : "baseUrl": "src", // must be the same as "rootDir" "paths": { "~/*": ["./*"] } 来源: https://stackoverflow.com/questions/53928378

Retrieve return type of function w/o calling the function

独自空忆成欢 提交于 2019-12-11 02:25:26
问题 Say I have a function like this in TypeScript: export const foo = function(){ return { a: 1, b: true, c: 'bar' } }; if I import this function into another file: import {foo} from './foobar'; My question is - is there a way to get the return type of foo without actually calling foo ? 回答1: This is now possible with Typescript 2.8 let foo = function() { return { a: 1, b: true, c: 'bar' } }; type ComplexObj = ReturnType<typeof foo>; // {a: number, b: boolean, c: string} 来源: https://stackoverflow

Error compiling Typescript with graphql files

天大地大妈咪最大 提交于 2019-12-10 22:25:05
问题 So I'm new in this area, the thing is that I'm trying to compile a Typescript project with graphql files on it (with .graphql extension). It' based on the serverless framework, so to compile it I launch npm start which launches a cd src/app && tsc in the command line. The .ts file references the .graphql file like this: import SchemaDefinition from './schemaDefinition.graphql'; And the error is data/schema/index.ts(2,30): error TS2307: Cannot find module './schemaDefinition.graphql'. I think

Share interfaces between separat TypeScript projects

倾然丶 夕夏残阳落幕 提交于 2019-12-10 19:28:06
问题 tl;dr at the bottom I've got a webserver written in TypeScript and a client SPA written in TypeScript. My idea was to share interfaces for request and response data to take full advantage of "type-safety". My big problem is that client and server are sibling folders each with its own package.json, tsconfig.json, src-dir etc. What I want to have is something like this: server/ ├── src/ ├── build/ ├── tsconfig.json └── package.json client/ ├── src/ ├── build/ ├── tsconfig.json └── package.json

How to export a TypeScript module for SystemJS?

梦想的初衷 提交于 2019-12-10 10:20:09
问题 On my website I want to import a FancyModule using SystemJS, so that I can create instances of classes which this module exports. So what I want to achieve is this (within a ECMAScript 2015-compatible website): SystemJS.import('MyFancyModule').then(function(MyFancyModule) { var myFancyInstance = new MyFancyModule.MyFancyClass('Test'); console.log(myFancyInstance.text); }); Unfortunately, I am getting the following error: TypeError: MyFancyModule.MyFancyClass is not a constructor(…) Here is my

Why is ES7/array polyfill needed despite the tsconfig target is set to ES5

眉间皱痕 提交于 2019-12-10 10:14:28
问题 I have the following settings in the tsconfig.json . I added "es2017" to use Array.includes .: { "compilerOptions": { "lib": [ "es6", "es2017", "dom" ], "module": "es6", "target": "es5" } } Now, I realized, that I have to add import 'core-js/es7/array'; to the polyfills.ts , to use Array.includes also for the Internet Explorer 11. The target in the tsconfig.json is set to es5 , which does not have Array.includes . Why do I need to add the polyfill? 回答1: TypeScript does not auto-polyfill code.

Mute/ignore TS2307 error from TypeScript tsc

最后都变了- 提交于 2019-12-09 14:22:03
问题 Is there a way to mute the TS2307 error from the TypeScript tsc compiler? It makes it really hard to spot real/new errors and warnings as there are many TS2307 errors in our codebase. Update : This error occurs when an external module is import ed without its type definition .d.ts being present. I'm aware of tsd but for many libraries we use, no type definitions exist. 回答1: No, there is not a way to direct the compiler to suppress TS2307. There has been some discussion about it for exactly

Map array to an interface

对着背影说爱祢 提交于 2019-12-09 13:41:59
问题 Say I have an array that looks like this: const options = [ { name: 'foo', type: 'boolean' }, { name: 'bar', type: 'string' }, { name: 'bar', // should be baz not bar type: 'number' } ] I am looking to use this array as an interface which would look something like this: export interface Opts { foo: boolean, bar: string, baz: number } so that would probably have to be something like: export type Opts = manipulate(typeof options); where manipulate is some magical TS feature I hope to discover.

How do I get TypeScript to bundle a 3rd party lib from node_modules?

守給你的承諾、 提交于 2019-12-08 17:06:30
问题 I would like the TypeScript Compiler to use node_modules/firebase/firebase.d.ts to typecheck my code and also bundle node_modules/firebase/firebase.js into some of the files where I import things from firebase. I understand there are many options which will do this for me, but I'd like to keep a minimal dev environment. I have set "moduleResolution": "node" in my tsconfig.json , which imports the definitions and type checks my code as I want. I've also added "isolatedModules": true in an

Importing ng2-bootstrap changes my tsc output directory structure

放肆的年华 提交于 2019-12-08 08:01:59
问题 I have the following folder structure dist src module1 m1.ts module2 m2.ts .tsconfig.json ... I have tsc configured to output to dist/js so after running npm run tsc I have the following: dist js module1 m1.js module2 m2.js src ... After much struggle I have managed to integrate ng-bootstrap into my project. If I import from ng2-bootstrap somewhere in my app, for example: import { ACCORDION_DIRECTIVES } from 'ng2-bootstrap/components/accordion'; and run npm run tsc my folder structure changes