question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

error TS2722: Cannot invoke an object which is possibly 'undefined'.

See original GitHub issue

🐞 Bug report

Command (mark with an x)

  • new
  • build
  • serve
  • test
  • e2e
  • generate
  • add
  • update
  • lint
  • xi18n
  • run
  • config
  • help
  • version
  • doc

Is this a regression?

yes.

Description

IN angular 10, below is my ts-config.

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "module": "es2020",
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "strictInjectionParameters": true,
    "strictTemplates": true
  }
}

when i try to serve my application i am facing below issue:

    ERROR in src/app/app.component.ts:8:14 - error TS2722: Cannot invoke an object which is possibly 'undefined'.

    8      <button (created)="oncreate($event)" ejs-button>Button</button>

But when i set strict:false it works fine.

πŸ”¬ Minimal Reproduction

  1. download sample from below link strict mode sample.zip
  2. Give npm i then ng serve

πŸ”₯ Exception or Error

D:\incidents\strict mode sample>ng serve
'wmic.exe' is not recognized as an internal or external command,
operable program or batch file.
(node:17616) ExperimentalWarning: The fs.promises API is experimental
Compiling @angular/core : es2015 as esm2015
Compiling @angular/animations : es2015 as esm2015
Compiling @angular/compiler/testing : es2015 as esm2015
Compiling @angular/animations/browser : es2015 as esm2015
Compiling @angular/common : es2015 as esm2015
Compiling @angular/core/testing : es2015 as esm2015
Compiling @angular/platform-browser : es2015 as esm2015
Compiling @angular/common/http : es2015 as esm2015
Compiling @angular/common/testing : es2015 as esm2015
Compiling @angular/platform-browser-dynamic : es2015 as esm2015
Compiling @angular/platform-browser/testing : es2015 as esm2015
Compiling @angular/router : es2015 as esm2015
Compiling @angular/forms : es2015 as esm2015
Compiling @angular/animations/browser/testing : es2015 as esm2015
Compiling @angular/common/http/testing : es2015 as esm2015
Compiling @angular/platform-browser/animations : es2015 as esm2015
Compiling @angular/platform-browser-dynamic/testing : es2015 as esm2015
Compiling @angular/router/testing : es2015 as esm2015
Compiling @syncfusion/ej2-angular-buttons : es2015 as esm2015
Compiling @syncfusion/ej2-angular-grids : es2015 as esm2015

chunk {main} main.js, main.js.map (main) 2 kB [initial] [rendered]
chunk {polyfills} polyfills.js, polyfills.js.map (polyfills) 677 bytes [initial] [rendered]
chunk {runtime} runtime.js, runtime.js.map (runtime) 6.15 kB [entry] [rendered]
chunk {styles} styles.js, styles.js.map (styles) 15 MB [initial] [rendered]
chunk {vendor} vendor.js, vendor.js.map (vendor) 341 kB [initial] [rendered]
Date: 2020-08-04T10:45:29.294Z - Hash: d04b0cca85382beb066d - Time: 29909ms

ERROR in src/app/app.component.ts:8:14 - error TS2722: Cannot invoke an object which is possibly 'undefined'.

8      <button (created)="oncreate($event)" ejs-button>Button</button>

🌍 Your Environment




"@angular/animations": "~10.0.6",
    "@angular/common": "~10.0.6",
    "@angular/compiler": "~10.0.6",
    "@angular/core": "~10.0.6",
    "@angular/forms": "~10.0.6",
    "@angular/platform-browser": "~10.0.6",
    "@angular/platform-browser-dynamic": "~10.0.6",
"@angular-devkit/build-angular": "~0.1000.5",
    "@angular/cli": "~10.0.5",
    "@angular/compiler-cli": "~10.0.6",

Anything else relevant?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
alan-agius4commented, Aug 4, 2020

@JoostK, maybe knows if we can do something around this.

1reaction
alan-agius4commented, Aug 4, 2020

In strict mode stricter type-checking rules are enabled. That being said the error here is not as clear as it should be, even though the error itself if emitted by TypeScript.

In /node_modules/@syncfusion/ej2-base/src/base.d.ts

export interface AngularEventEmitter {
    subscribe?: (generatorOrNext?: any, error?: any, complete?: any) => any;
}

The subscribe is optional and hence it is possibly undefined, as such the error is genuine.

Transferring it to the framework repo.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot invoke an object which is possibly 'undefined'.ts(2722)
The above solution is too much confusing I was getting the error Cannot invoke an object which is possibly 'undefined.
Read more >
Cannot invoke an object which is possibly 'undefined' in TS
The error "Cannot invoke an object which is possibly 'undefined'" occurs when we try to invoke a function property that could be undefined...
Read more >
TSC Strict mode error TS2722: Cannot invoke an object which ...
Expected behavior: No error is thrown by tsc compiler. Actual behavior: src/application-insights/ApplicationInsights.ts(25,17): error TS2722 ...
Read more >
Error: Cannot invoke an object which is possibly 'undefined ...
Error: Cannot invoke an object which is possibly 'undefined'. from react-table How can I fix it? Β· You can check whether the function...
Read more >
cannot invoke an object which is possibly 'undefined' typescript
A clear and concise description of the problem… I am trying to use strictTemplates atm an encountered a weird behaviour. In the below...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found