ts-node 7 failing to use locally defined types

Created on 25 Jun 2018  Â·  7Comments  Â·  Source: TypeStrong/ts-node

Something that changed between ts-node 6.1.0 and 7.0.0 broke a previously working project (or revealed a long-standing bug in my code).

In app/server/index.ts I have:

import * as next from 'next'

types for which are defined - as far as I can tell, correctly - in a local, hand-crafted d.ts file in /app/@types/.

tsconfig.server.json is:

{
  "extends": "./tsconfig.json", 
  "compilerOptions": {
    "module": "commonjs",
    "jsx": "react",
    "outDir": "dist/"
  },
  "include": ["app/server/**/*.ts", "./app/@types/**/*.ts"]
}

which extends tsconfig.json:

{
  "compileOnSave": false,
  "compilerOptions": {
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "baseUrl": ".",
    "jsx": "preserve",
    "lib": ["dom", "es2016"],
    "module": "esnext",
    "moduleResolution": "node",
    "noFallthroughCasesInSwitch": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "preserveConstEnums": true,
    "removeComments": false,
    "skipLibCheck": true,
    "sourceMap": true,
    "strict": true,
    "target": "esnext",
    "typeRoots": ["./node_modules/@types", "./lib/@types", "./app/@types"]
  }
}

under ts-node 6.1.0, the application starts as expected. Under ts-node 7.0.0, I get the following error:

TSError: ⨯ Unable to compile TypeScript:
app/server/index.ts(6,23): error TS7016: Could not find a declaration file for module 'next'. '/usr/src/service/node_modules/next/dist/server/next.js' implicitly has an 'any' type.

Were there significant behaviour changes between 6.1.0 and 7.0.0, or did something get stricter? I can't see a changelog anywhere. I _suspect_ it might be due to some error in the declarations file, but I can't see one.

invalid

Most helpful comment

for gulp users, add process.env['TS_NODE_FILES'] = true is to enable loading files from tsconfig.json.

gulp.task('test', function() {
  let target = 'test/*.spec.ts'
  process.env['TS_NODE_FILES'] = true
  return gulp
    .src([target], { read: false })
    .pipe(
      mocha({
        reporter: 'list',
        require: ['ts-node/register'],
        timeout: '120000'
      })
    )
    .on('error', gutil.log)
})

All 7 comments

I'm having the same issue with a nearly identical setup to @majelbstoat.
The changes from 6.2.0 to 7.0.0 are here https://github.com/TypeStrong/ts-node/commit/33cb1b5faecaa652b70fe97cb3665de7d42565c2 and the changelog message reads

Skip loading tsconfig.json files by default to speed up startup

Given this change, how are local/custom types intended to be referenced?

@mastermatt @majelbstoat putting my .d.ts files at the root of my project (with tsconfig.json and package.json) did the job for me.

That's helpful to know @lucasriondel, thanks. However, I also symlink in a directory of common types from elsewhere in a mono-repo, so i need to be able to specify a path to them.

Using the recently added --files flag solved this issue for me. (Note that the flag behavior was inverted after v7.0.0)

for gulp users, add process.env['TS_NODE_FILES'] = true is to enable loading files from tsconfig.json.

gulp.task('test', function() {
  let target = 'test/*.spec.ts'
  process.env['TS_NODE_FILES'] = true
  return gulp
    .src([target], { read: false })
    .pipe(
      mocha({
        reporter: 'list',
        require: ['ts-node/register'],
        timeout: '120000'
      })
    )
    .on('error', gutil.log)
})

Using the recently added --files flag solved this issue for me. (Note that the flag behavior was inverted after v7.0.0)

Thank you so much. it worked! I thought the problem was the typescript compiler

Was this page helpful?
0 / 5 - 0 ratings

Related issues

motss picture motss  Â·  4Comments

sodiumjoe picture sodiumjoe  Â·  4Comments

watzon picture watzon  Â·  3Comments

cevek picture cevek  Â·  4Comments

aj-r picture aj-r  Â·  3Comments