lit / lit/lit.dev

Add example workaround for conditional attributes when using unsafeStatic

Open
#1,177 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
142
Forks
230
PR merge metrics
No merged PRs in 30d

Description

Trying to use `ifDefined` inside of `unsafeStatic` causes a type error from TS and a runtime error from JS: “Cannot convert a Symbol value to a string”. This wasn't immediately apparent to us and, in our case, our editors (Webstorm and VSCode) did not flag the type conversion error from TS at all when the invalid usage is wrapped inside of a `` element.

The workaround is to use a conditional to determine whether to render the attribute:
```typescript
import { html, unsafeStatic } from 'lit/static-html';

@property()
type?: string;

render() {
return html`
${unsafeStatic(`
some text
`)}
`
}
```

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No documentation file or test is named. Start from the unsafeStatic documentation entry point and review the TypeScript example in this issue; done means documenting the conditional-attribute workaround and explaining the failing ifDefined usage and its errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.