Skip to content

Commit

Permalink
feat(task): improve type checking for tuples (lit#4836)
Browse files Browse the repository at this point in the history

---------

Co-authored-by: Augustine Kim <[email protected]>
  • Loading branch information
maxpatiiuk and Augustine Kim authored Nov 26, 2024
1 parent 37c0250 commit 05691ba
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-rabbits-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lit/task': patch
---

Improve type inference of tuples returned by the args function being used as task function parameter.
2 changes: 1 addition & 1 deletion packages/labs/task/src/test/task_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ suite('Task', () => {
});

test('Elements only render once for pending tasks', async () => {
let resolveTask: (v: unknown) => void;
let resolveTask: (v: any) => void;
let renderCount = 0;
class TestElement extends ReactiveElement {
task = new Task(this, {
Expand Down
6 changes: 3 additions & 3 deletions packages/task/src/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ export interface TaskConfig<T extends ReadonlyArray<unknown>, R> {
* ```
*/
export class Task<
T extends ReadonlyArray<unknown> = ReadonlyArray<unknown>,
R = unknown,
const T extends ReadonlyArray<unknown> = ReadonlyArray<unknown>,
const R = unknown,
> {
private _previousArgs?: T;
private _task: TaskFunction<T, R>;
Expand Down Expand Up @@ -227,12 +227,12 @@ export class Task<
private _rejectTaskComplete?: (e: unknown) => void;
private _taskComplete?: Promise<R>;

constructor(host: ReactiveControllerHost, task: TaskConfig<T, R>);
constructor(
host: ReactiveControllerHost,
task: TaskFunction<T, R>,
args?: ArgsFunction<T>
);
constructor(host: ReactiveControllerHost, task: TaskConfig<T, R>);
constructor(
host: ReactiveControllerHost,
task: TaskFunction<T, R> | TaskConfig<T, R>,
Expand Down
29 changes: 28 additions & 1 deletion packages/task/src/test/task_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ suite('Task', () => {
});

test('Elements only render once for pending tasks', async () => {
let resolveTask: (v: unknown) => void;
let resolveTask: (v: any) => void;
let renderCount = 0;
class TestElement extends ReactiveElement {
task = new Task(this, {
Expand Down Expand Up @@ -1072,4 +1072,31 @@ suite('Task', () => {
})
);
});

test('tuple type arguments are inferred without need for "as const"', () => {
const expectType = <T>(x: T) => x;

class TestElement extends ReactiveElement {
string = '';
number = 1;

task = new Task(
this,
([string, number]) => {
expectType<string>(string);
expectType<number>(number);
},
() => [this.string, this.number]
);

task2 = new Task(this, {
task: ([string, number]) => {
expectType<string>(string);
expectType<number>(number);
},
args: () => [this.string, this.number],
});
}
TestElement;
});
});

0 comments on commit 05691ba

Please sign in to comment.