Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complains about "object is possibly 'undefined'" when it should not #50

Open
boris-petrov opened this issue Feb 6, 2021 · 11 comments · Fixed by #55
Open

Complains about "object is possibly 'undefined'" when it should not #50

boris-petrov opened this issue Feb 6, 2021 · 11 comments · Fixed by #55
Labels
bug Something isn't working question Further information is requested

Comments

@boris-petrov
Copy link

import Component from '@glimmer/component';

export default class GlimmerComponent extends Component {
  public foo?: { bar: any };
}
{{#if this.foo.bar}}
{{/if}}

This leads to:

Object is possibly 'undefined'.typed-templates

Which is kind of useless - HBS supports getting properties on undefined objects - that's a feature, not a bug.

There is another case worth mentioning:

{{some-helper-that-accepts-undefined this.foo.bar}}
{{some-helper-that-does-NOT-accept-undefined this.foo.bar}}

The first line should be fine but the second one should err if possible.

@boris-petrov
Copy link
Author

Third example:

public arr?: readonly string[];
{{#each this.arr as |value index|}}
{{/each}}

Again complains about the possibility arr to be undefined.

@lifeart
Copy link
Owner

lifeart commented Feb 7, 2021

Likely it should be an warning, not error

@lifeart lifeart mentioned this issue Feb 7, 2021
@lifeart
Copy link
Owner

lifeart commented Feb 7, 2021

this errors now warnings (4.0.6)

@lifeart lifeart added bug Something isn't working question Further information is requested labels Feb 7, 2021
@boris-petrov
Copy link
Author

@lifeart - thanks for all the quick fixes and changes! However, in this case, I disagree with what you've done. As I mentioned in my first post, HBS' handling of undefined is actually a feature. So having this.foo.bar.baz.qux, when any of the parts could return undefined, the type of the whole thing should also include undefined (in addition to what type qux has) and that should definitely be neither an error, nor a warning. Or at least provide an option for that - but again, this is a feature of HBS so most people are likely depending on it. So please do not err or warn for that. 😄

@boris-petrov
Copy link
Author

Also, the third example I gave still errs - #each works fine with undefined as a first parameter (and is expected to, as it even has an else clause because of that) but els-addon-typed-templates complains about it.

@lifeart
Copy link
Owner

lifeart commented Feb 8, 2021

@boris-petrov I'm getting your points accroding to handlebars undefined handing feature, but I'm not shure we have to follow it in typed way, we could ask question in emberjs/rfcs#591 and in related typescript chatter in discord (https://discord.com/channels/480462759797063690/484421406659182603) to agree on path forward for given case.

@lifeart
Copy link
Owner

lifeart commented Feb 8, 2021

conversation: https://discord.com/channels/480462759797063690/484421406659182603/808348456045379584

as I get:
we have to use optional chaining, but, all invocable items should be defined

@lifeart
Copy link
Owner

lifeart commented Feb 8, 2021

@boris-petrov could you check els-addon-typed-templates@4.1.0?

@boris-petrov
Copy link
Author

@lifeart - I think this version broke a bunch of other things:

public get columns(): readonly string[] {
  return [];
}

@action
public onClick(): void {
}
{{#each this.columns as |column|}}
{{/each}}

<button type="button" {{on "click" this.onClick}}>
</button>

These give the following two errors:

Type 'readonly string[] | undefined' is not assignable to type 'ArrayLike<string>'.
  Type 'undefined' is not assignable to type 'ArrayLike<string>'.typed-templates

Type '(() => void) | undefined' is not assignable to type 'Function'.
  Type 'undefined' is not assignable to type 'Function'.typed-templates

@lifeart
Copy link
Owner

lifeart commented Feb 8, 2021

ya, that’s I’m talking about, both issue could be fixed as typing updates, but it will produce less maintainable and checkable code, looks like we need to refigure out how to handle it

@lifeart
Copy link
Owner

lifeart commented Feb 8, 2021

@boris-petrov for now I'm rolling back changes in 4.2.0
issue for context-related paths created - #57

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants