Skip to content

[labs/context] @provide forces a non-optional property #3732

@jpzwarte

Description

@jpzwarte

Which package(s) are affected?

Context (@lit-labs/context)

Description

The code is this:

  @provide({ context: studentContext })
  @property({ type: Object })
  student?: Student;

The typing error I get with this:

Argument of type 'Root' is not assignable to parameter of type 'ReactiveElement & Record<"student", Student>'.
  Type 'Root' is not assignable to type 'Record<"student", Student>'.
    Types of property 'student' are incompatible.
      Type 'Student | undefined' is not assignable to type 'Student'.
        Type 'undefined' is not assignable to type 'Student'.ts(2345)

If I make the property not optional (so remove the question mark), then I get this typing error:

Property 'student' has no initializer and is not definitely assigned in the constructor.ts(2564)

"Damned if I do, damned if I don't"....

Reproduction

Workaround

I've tried this, to no avail: export const studentContext = createContext<Student | undefined>('student');

If I do the above, then I do get a different typing error:

Argument of type 'Root' is not assignable to parameter of type 'ReactiveElement & Record<"student", Student | undefined>'.
  Type 'Root' is not assignable to type 'Record<"student", Student | undefined>'.
    Property 'student' is optional in type 'Root' but required in type 'Record<"student", Student | undefined>'.ts(2345)

Is this a regression?

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

Affected versions

"@lit-labs/context": "^0.2.0",

Browser/OS/Node environment

"typescript": "^4.9.5" with "strict": true, in tsconfig.json

Metadata

Metadata

Assignees

No one assigned

    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