You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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'.
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.
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.
Activity
oliversalzburg commentedon Jun 17, 2022
Maybe the code in the implementation is actually wrong. If I make this
RecordaPartial<Record<...>>, the error goes away: https://github.com/lit/lit/blob/main/packages/labs/context/src/lib/decorators/context-provided.ts#L50drk-mtr commentedon Jun 27, 2022
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 forcontextProvided.oliversalzburg commentedon Jun 27, 2022
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.
drk-mtr commentedon Jun 27, 2022
Yep I was suggesting this as a workaround rather than good practice :)
justinfagnani commentedon Oct 27, 2022
I have a fix for this in #3398