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.

Truthy value check, but red highlighting for undefined

See original GitHub issue

Describe the bug

If you check a variable in HTML has a truthy value (not null, not undefined, …) and call then a method on it, I get an error with unknown method because VSCode believes object can be undefined.

Example:

{{ foo && foo.bar() }}

In my example, I consider foo : Type | undefined;

To Reproduce

Just implement a similar simple foo attribute in an Angular component and try to call a method in the template checking truthy first.

Expected behavior

No red highlighting as value is checked using foo &&

Screenshots

image

In this case, startModelInstance is defined as public startModelInstance?: ModelInstance;

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
andrius-pracommented, Jan 28, 2020

you can use this example:

import { Component } from '@angular/core';
@Component({
  selector: 'issue',
  template: `
    {{ element && element.blur() }}
                  ~~~~~~~~~~~~~~~
                  Unknown method 'blur'

  `,
})
export class ApiListComponent {
  element?: HTMLInputElement;
}
1reaction
andrius-pracommented, Jan 29, 2020

I was also unable to reproduce the error with the example that @andrius-pra showed.

Use strict mode in tsconfig:

  "compilerOptions": {
    "strict": true
  },
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using a truthy/falsy check in Typescript vs explicit check ...
I understand that null and undefined are different. I understand that the !myBar.foo is a broader check in normal Javascript. But in TS-land, ......
Read more >
VS Code API | Visual Studio Code Extension API
For some languages one color can have multiple presentations, e.g. css can represent the color red with the constant Red , the hex-value...
Read more >
CodeMirror 5 User Manual
The value "null" indicates no highlighting should be applied. Alternatively, it may be an object containing configuration options for the mode, ...
Read more >
Manage Symbols in the Stateflow Editor - MATLAB & Simulink
Resolve Symbols Through the Symbols Pane. As you edit your chart, Stateflow detects undefined symbols and marks them in the Symbols pane with...
Read more >
nunjucks templating docs
If a value is undefined or null , nothing is displayed. The same behavior occurs when referencing undefined or null objects. The following...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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