Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for typeRoots? #421

Closed
mikemichaelis opened this issue Sep 26, 2016 · 11 comments
Closed

Support for typeRoots? #421

mikemichaelis opened this issue Sep 26, 2016 · 11 comments
Labels

Comments

@mikemichaelis
Copy link

mikemichaelis commented Sep 26, 2016

When compiling with gulp-typescript I receive the error

error TS2304: Cannot find name 'Promise' (or Set or Map)

Having researched this issue I discovered the new TS compilerOption typeRoots.

http://stackoverflow.com/questions/35660498/angular-2-cant-find-promise-map-set-and-iterator

Properly setting this option in tsconfig.json and manually executing tsc, I no longer receive this error message. Unfortunately gulp-typescript does not support typeRoots.

Expected behavior:
Compile w/o error TS2304

Actual behavior:
/home/mike/Projects/rsvt/node_modules/@angular/core/src/facade/collection.d.ts(16,25): error TS2304: Cannot find name 'Map'.
[15:48:20] TypeScript error: /home/mike/Projects/rsvt/node_modules/@angular/core/src/facade/collection.d.ts(16,25): error TS2304: Cannot find name 'Map'.
/home/mike/Projects/rsvt/node_modules/@angular/core/src/facade/collection.d.ts(101,41): error TS2304: Cannot find name 'Set'.
[15:48:20] TypeScript error: /home/mike/Projects/rsvt/node_modules/@angular/core/src/facade/collection.d.ts(101,41): error TS2304: Cannot find name 'Set'.

Your gulpfile:

Include your gulpfile, or only the related task (with ts.createProject).

gulp.task('ts', function() {
    var src = config.appDir + '**/*.ts';
    log('Compiling ' + src);

    var tsProject = $.typescript.createProject(config.tsconfig);

    return gulp
        .src(src)
        .pipe($.plumber({
            errorHandler: error
        }))
        .pipe($.if(args.verbose, $.print()))
        .pipe($.if(config.sourcemaps, $.sourcemaps.init()))
        // .pipe($.typescript(tsProject))
        .pipe(tsProject())
        .pipe($.if(config.sourcemaps, $.sourcemaps.write('.')))
        .pipe(gulp.dest(config.appDir));
});

tsconfig.json

Include your tsconfig, if related to this issue.

{
    "compilerOptions": {
        "target": "es5",
        "module": "system",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": false,
        "noImplicitAny": false,
        "typeRoots": [
            "node_modules/@types"
        ]
    },
    "types": [ "core-js" ],
    "exclude": [
        "node_modules",
        "bower_components",
        "src/jspm_packages",
        "typings/main",
        "typings/main.d.ts"
    ]
}

It would appear my problem is similar to
lib compilerOptions not used? #420
but resolved using a different TS 2.0 supported config option.

@mikemichaelis
Copy link
Author

mikemichaelis commented Sep 29, 2016

If anybody else needs the solution it is to switch to gulp-tsc as I could never get gulp-typescript to work at all using Angular 2.0 TS 2.0. There were a few other @types I needed to install such as @types/jasmine @types/node, however gulp-typescript will not use these type definitions regardless. gulp-tsc also has the ability to pass to tsc any parameter you want using options.additionalTscParameters, which is awesome.

project/tsc <-- works great using tsconfig.json w/o typeRoots or types specified.

Here's my new gulpfile.js for TS w/ gulp-tsc

gulp.task('ts', function() {
var src = config.appDir + '**/*.ts';
log('Compiling ' + src);

var tsc = $.tsc;

return gulp
    .src(src)
    .pipe($.plumber({
        errorHandler: error
    }))
    .pipe($.if(args.verbose, $.print()))
    .pipe(tsc( { experimentalDecorators: true }))
    .pipe($.if(config.sourcemaps, $.sourcemaps.write('.')))
    .pipe(gulp.dest(config.appDir));

My old gulp-typescript gulpfile..js which does not work

gulp.task('ts', function() {
var src = config.appDir + '**/*.ts';
log('Compiling ' + src);

var tsProject = $.typescript.createProject(config.tsconfig);

return gulp
    .src(src)
    .pipe($.plumber({
        errorHandler: error
    }))
    .pipe($.if(args.verbose, $.print()))
    .pipe($.if(config.sourcemaps, $.sourcemaps.init()))
    // .pipe($.typescript(tsProject))
    .pipe(tsProject())
    .pipe($.if(config.sourcemaps, $.sourcemaps.write('.')))
    .pipe(gulp.dest(config.appDir));

@ivogabe
Copy link
Owner

ivogabe commented Sep 29, 2016

I'll take a look at this issue this weekend.

@ivogabe ivogabe added the Bug label Sep 29, 2016
@ivogabe
Copy link
Owner

ivogabe commented Oct 23, 2016

I think that this has been solved in d706e30. Can you try whether gulp-typescript 3.0.2 solves your issues? Also, you can probably remove typeRoots: ["node_modules/@types"], since that's the default.

@kkganesan
Copy link

👍 @ivogabe. 3.0.2 fix worked.

@teetotum
Copy link

teetotum commented Dec 2, 2016

I discovered an issue with gulp-typescript version 3.0.2 and the typeRoots option:
It will run into an error if subfolders exist.

Observed Issue:
error TS2688: Cannot find type definition file for 'foo'

My folder structure for my project:

[MyProject]
  -[TypeScript] # *.ts sourcecode goes here
  -[typings] # *.d.ts type-definitions here
    -[foo] # type-definition subfolder, might contain *.d.ts or might be empty, still causes observed error
      -[bar]
      -[baz]

My tsconfig.json

{
  "compilerOptions": {
    "module": "amd",
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "preserveConstEnums": true,
    "sourceMap": true,
    "target": "es5",
    "typeRoots": [ "typings" ],
    "rootDir": "TypeScript"
  },
  "exclude": [
    "node_modules",
    "wwwroot"
  ],
  "compileOnSave": false
}

My gulp task for typescript compilation:

var sourcemaps = require("gulp-sourcemaps");
var typescript = require("gulp-typescript");
var tsProject = typescript.createProject("tsconfig.json");
gulp.task("compile:typescript", function () {
	var tsResult = tsProject
		.src() // instead of gulp.src(...)
		.pipe(sourcemaps.init())
        .pipe(tsProject());
	return tsResult.js
		.pipe(sourcemaps.write(".",
		{
			includeContent: false,
			sourceRoot: "../../TypeScript/"
		}))
		.pipe(gulp.dest(paths.javascriptDestination)); // destination is ./wwwroot/scripts
});

I can fix the problem for me if I change the typeRoots option in tsconfig.json to "typeRoots": [ "typings/**/*" ],

Expected Behavior
The typeRoots option should only expect the root folders for type-def files to be specified and automatically include all subfolders, and not encounter an error if one of those subfolders does not contain any *.d.ts files.

@ivogabe
Copy link
Owner

ivogabe commented Dec 3, 2016

@teetotum Does the behaviour of gulp-typescript differ from tsc? I'd expect that they behave the same way.

@teetotum
Copy link

teetotum commented Dec 8, 2016

Oh diddums! Just tested my setup with a direct call to tsc.exe (2.0) and I get exactly the same error. Sorry for bringing the issue up here. gulp-typescript behaves correctly.

@ivogabe ivogabe closed this as completed Dec 11, 2016
@mikaye
Copy link

mikaye commented May 5, 2017

Not sure if this ever got resolved. It appears to me that gulp-tsc uses some options form tsconfig.json but not others. Fore example, outDir was taken from tsconfig.json whereas sourceMap was ignored and I had to include in the options. typeRoots is ignored even if I include it in typescript options. Of course, tsc works fine from the terminal

Any thoughts on how to get tsc working from gulp with typings in ./typings?

var typescript = require('gulp-tsc');
gulp.task('compile', function () {
  gulp.src(['electron_src/**/*.ts'])
    .pipe(typescript({ tscPath: "/usr/local/bin/tsc", sourceMap: true, "typeRoots": ["./typings/globals"]}))
    .pipe(gulp.dest('build/electron'))
});

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false,
    "suppressImplicitAnyIndexErrors": true,
    "typeRoots": ["./typings/globals"],        
    "outDir": "build/electron"
  },
  "exclude": [
    "node_modules"
  ]
}

@ivogabe
Copy link
Owner

ivogabe commented May 9, 2017

@mikaye This is the repository for gulp-typescript, but your post references gulp-tsc. gulp-typescript supports typeRoots, but I think that gulp-tsc should also support that options. If you need help with gulp-tsc you can better ask on some other place.

@selmalee
Copy link

this bug still exists. gulp-typescript@6.0.0-alpha.1

gulp-typescript does not support typeRoots in tsconfig.json.

@LikeDege
Copy link

@seminelee how to resolve this problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants