Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File is not included in the TypeScript compilation context

Using Atom editor with the typescript plug I'm getting the following error:

Error The file "D:/foo/app/classes/event.class.ts" is not included in the TypeScript compilation context. If this is not intended, please check the "files" or "filesGlob" section of your tsconfig.json file.at line 1 col 1

The contents of my tsconfig.json file is:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
    ]
}

I have looked at a few others that have had this problem. This: https://github.com/lathonez/clicker/issues/4 got me to try building a "files" array in the tsconfig.json array. That, much like with the person in the other thread, did not help. Note, that thread talked a lot of about testing... which doesn't apply for me.

I also tried parsing through this thread: https://github.com/TypeStrong/atom-typescript/issues/558 however that basically ended up being an argument about purity vs pragmatism. I did pick up that if the files and filesGlob arrays are missing, an implicit "everything" glob is used. If this were the case, why am I getting the error given I don't have a file and filesGlob entry.

As an aside, command-line TSC is generating the compiled js and map files just fine. ... but I still have to see the big red error in Atom.

For what it's worth, the event.class.ts file looks like this (I don't expect this is the source of the problem, but figured I'd include it for completeness):

import {Utilities} from '../utilities';

export class Event {
    eventData:JSON;
    eventID:string;

    constructor(_eventData:JSON, _eventID?:string) {
        if(_eventID==null) {
            _eventID=Utilities.newGuidPlus();
        }
        this.eventID = _eventID;
        this.eventData = _eventData;
    }


    getEventData():JSON { // returns the full event
        return this.eventData;
    }

    getEventID():string {
        return this.eventID;
    }
}
like image 296
lowcrawler Avatar asked Jun 15 '16 19:06

lowcrawler


People also ask

What is the compilation context in TypeScript?

The compilation context is a term for a grouping of the TypeScript files that will parse and analyze to determine what is valid and what is not valid. The compilation context contains the information about which compiler options are in use. We can define this logical grouping of TypeScript files by using a tsconfig.

Why do we need Tsconfig JSON?

json file is a file of JSON format which allows us to point the root level files and different compiler options to setup that require to compile a TypeScript based projects. The existence of this file in a project specifies that the given directory is the TypeScript project folder root.

What is the role and structure of Tsconfig JSON?

The tsconfig.json file specifies the root files and the compiler options required to compile the project. JavaScript projects can use a jsconfig.json file instead, which acts almost the same but has some JavaScript-related compiler flags enabled by default.


2 Answers

For the exact issue in my case, I included "**/*.ts" in “filesGlob” property in tsconfig.json to resolve. The whole tsconfig.json looks like this.

{
  "compileOnSave": false,
  "compilerOptions": {
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "mapRoot": "/",
    "module": "commonjs",
    "moduleResolution": "node",
    "noEmitOnError": true,
    "noImplicitAny": false,
    "outDir": "../dist/",
    "rootDir": ".",
    "sourceMap": true,
    "target": "es5",
    "inlineSources": true
  },

  "filesGlob": [
    "**/*.ts"
  ],

  "atom": {
    "rewriteTsconfig": false
  }
}
like image 180
Rohan_Paul Avatar answered Oct 27 '22 20:10

Rohan_Paul


For my case, the solution was different:

  1. Ensure that ALL files in your project have ALL the imports well written. (Case sensitive)
  2. Save all files. Close Atom. Restart your Computer. (Not necessary)
  3. Open Atom again. Done. :)

I will update if something else occurs with this bug.

like image 41
Ciberman Avatar answered Oct 27 '22 18:10

Ciberman