Skip to content

[lit-html] when directive should have better type def #4085

@megheaiulian

Description

@megheaiulian

Should this be an RFC?

  • This is not a substantial change

Which package is this a feature request for?

Lit Core (lit / lit-html / lit-element / reactive-element)

Description

It would be useful for the when directive to guard and properly type its checked value.
For example:

const val: number|null|undefined = 2;
const  fn = (v: number)=>v;
when(
val,
 // type error because val can be undefined
 ()=>fn(val) 
)

In reality val can't be undefined because of the checks in when.

Alternatives and Workarounds

A possible non breaking change solution could be to pass our guard types thru a param to the trueCase and falseCase fn.

const val: number|null|undefined = 2;
const  fn = (v: number)=>v;
when(
val,
 // when passes value to trueCase and `v`  is Exclude<typeof val, undefined|null|false>
 (v)=>fn(v) 
)

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