Skip to content

[labs/context] @contextProvided doesn't work well with optional fields #3314

@justinfagnani

Description

@justinfagnani

Which package(s) are affected?

Context (@lit-labs/context)

Description

It's not possible to use optional fields with @contextProvided because of the use of Record in the decorator types.

This should work, since it's always possible that there is no provider for a context key:

const fooContext = createContext<Foo>('foo');

class MyElement extends LitElement {
  @contextProvided({context: fooContext})
  foo?: Foo;
}

Reproduction

See above

Workaround

Mark fields and context keys as X | undefined instead of optional.

Is this a regression?

No or unsure. This never worked, or I haven't tried before.

Affected versions

all

Browser/OS/Node environment

all

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Status

✅ Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions