Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

After upgrade to Angular 9 cannot find variable in typings file

I have a reference to an external logging component (referenced in a js file), which I have defined in a typings file

typings.d.ts

declare var LogglyTracker;

I have upgraded my angular application from version 8 to 9, and now when I run ng build, I get the following error

ERROR in src/app/error/loggly.service.ts:13:29 - error TS2304: Cannot find name 'LogglyTracker'.

Does anyone know what the recommended way to fix this problem is? This code worked fine in Angular 8.

@Injectable()
export class LogglyLoggerService {

// https://github.com/loggly/loggly-jslogger
private loggly: any = new LogglyTracker(); //!! Error here
}
like image 386
jazza1000 Avatar asked Jan 22 '20 11:01

jazza1000


2 Answers

Had the same problem after migrating from Angular 7 to Angular 9.

I personally just had to add one line in tsconfig.app.json.

Before:

{
    "extends": "../tsconfig.json",
    "compilerOptions": {
        "outDir": "../out-tsc/app",
        "types": [
            "node"
        ]
    },
    "files": [
        "main.ts",
        "polyfills.ts"
    ],
    "include": [
        "src/**/*.d.ts"
    ]
}

After:

{
    "extends": "../tsconfig.json",
    "compilerOptions": {
        "outDir": "../out-tsc/app",
        "types": [
            "node"
        ]
    },
    "files": [
        "main.ts",
        "polyfills.ts"
    ],
    "include": [
        "src/**/*.d.ts",
        "typings.d.ts" // add this line
    ]
}

I assumed you already made changes in tsconfig.json before migrating to Angular 9. If not, here it is:

{
    ...
    "compilerOptions": {
        ...
        "typeRoots": [
            "node_modules/@types",
            "src/typings.d.ts"
        ],
        ...
    }
}
like image 123
Emerica Avatar answered Nov 15 '22 20:11

Emerica


I had exactly the same problem as the author - after upgrade to Angular 9, my local type definitions were no longer found.

In Angular 8, I added references to them in tsconfig.json

"typeRoots": [
    "node_modules/@types",
    "../src/typings/my-lib/index.d.ts"
    "node_modules/@types"
  ]

After upgrading to Angular 9, my-lib was no longer found. I generated a new app using Angular 9 CLI, and found that 2 files reside in different locations:

  • src/tsconfig.app.json -> tsconfig.app.json
  • src/tsconfig.spec.json -> tsconfig.spec.json

I made similar change in my migrated project, updated references to these files in angular.json, and corrected pointers to other files in tsconfig.app.json and tsconfig.spec.json

As a result, my tsconfig.app.json looks like:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/app",
    "types": ["jest"]
  },
  "files": [
    "src/main.ts",
    "src/polyfills.ts"
  ],
  "include": [
    "src/**/*.d.ts"
  ]
}

Note that all d.ts files placed under src directory will be pulled in automatically (no reference in typeRoots section of tsconfig.json is necesssary)

like image 4
Lesiak Avatar answered Nov 15 '22 18:11

Lesiak