Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates task.md to include typescript as const tip. #1329

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions packages/lit-dev-content/site/docs/v3/data/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,3 +442,41 @@ class MyElement extends LitElement {
```

{% endswitchable-sample %}

### TypeScript Tips
justinfagnani marked this conversation as resolved.
Show resolved Hide resolved

justinfagnani marked this conversation as resolved.
Show resolved Hide resolved
Consider the following task, with two arguments.

```ts
class MyElement extends LitElement {
@property() myNumber = 10;
@property() myText = "Hello world";

_myTask = new Task(this, {
args: () => [this.myNumber, this.myText],
task: ([number, text]) => {
// implementation omitted
}
});
}
```

As written, the type of the argument list to the task function is inferred as `Array<number | string>`.

But ideally this would be typed as a tuple `[number, string]` because the size and position of the args is fixed.

The return value of `args` can be written as `args: () => [this.myNumber, this.myText] as const`, which will result in a tuple type for the args list to the `task` function.

```ts
class MyElement extends LitElement {
@property() myNumber = 10;
@property() myText = "Hello world";

_myTask = new Task(this, {
args: () => [this.myNumber, this.myText] as const,
task: ([number, text]) => {
// implementation omitted
}
});
}
```