Skip to content

[labs/context]: Invalid example code #3058

Closed

Description

@oliversalzburg

Description

The example given under "Consuming a context" is invalid. The code raises a TypeScript error.

Steps to Reproduce

The code in the example already produces the error. It also imports contextRequest and then uses contextProvided.

Expected Results

The example code should be valid.

Actual Results

Argument of type 'Logger' is not assignable to parameter of type 'ReactiveElement & Record<"logger", Logger>'.
  Type 'Logger' is not assignable to type 'Record<"logger", Logger>'.
    Types of property 'logger' are incompatible.
      Type 'Logger | undefined' is not assignable to type 'Logger'.
        Type 'undefined' is not assignable to type 'Logger'.

Browsers Affected

  • Chrome
  • Firefox
  • Edge
  • Safari 11
  • Safari 10
  • IE 11

Activity

  1. oliversalzburg commented on Jun 17, 2022

    @oliversalzburg
    Author

    Maybe the code in the implementation is actually wrong. If I make this Record a Partial<Record<...>>, the error goes away: https://github.com/lit/lit/blob/main/packages/labs/context/src/lib/decorators/context-provided.ts#L50

  2. moved this to 📋 Triaged in Lit Project Boardon Jun 18, 2022
  3. drk-mtr commented on Jun 27, 2022

    @drk-mtr

    Doing this from memory so apologies if I'm sharing bad info. It may be as simple as changing logger? -> logger! - and obviously updating the import for contextProvided.

  4. oliversalzburg commented on Jun 27, 2022

    @oliversalzburg
    Author

    I thought it's marked as optional because the context might not actually have been provided elsewhere in the application or while the context hasn't been connected yet. At least it made sense to me that way.

    The later usage of this.logger?.log() also indicates that this is intentional and nulish values are to be expected.

    Beyond that, I never use non-null assertions on members that aren't defined after construction of the object. That just seems needlessly unsafe.

  5. drk-mtr commented on Jun 27, 2022

    @drk-mtr

    Yep I was suggesting this as a workaround rather than good practice :)

  6. self-assigned this
    on Sep 8, 2022
  7. justinfagnani commented on Oct 27, 2022

    @justinfagnani
    Collaborator

    I have a fix for this in #3398

  8. Repository owner moved this from 📋 Triaged to ✅ Done in Lit Project Boardon Nov 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions