Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion table/src/-private/-type-tests/cell-args.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { meta } from '../../plugins/index.ts';

import type { CellContext, Column, ColumnConfig, Table } from '../../index.ts';
import type { ComponentLike } from '@glint/template';
import type { CellComponent } from '../cell-component.ts';

interface Person {
name: string;
Expand All @@ -37,7 +38,11 @@ declare const UpdateCell: ComponentLike<
declare const PlainCell: ComponentLike<CellContext<Person>>;

type CellArgsOf<Cell> =
NonNullable<Cell> extends ComponentLike<infer Args> ? Args : never;
NonNullable<Cell> extends CellComponent<infer Args>
? Args
: NonNullable<Cell> extends ComponentLike<infer Args>
? Args
: never;

/////////////////////////////////////////////
// The args of the Cells are inferred, besides `@row` and `@column`
Expand Down
32 changes: 32 additions & 0 deletions table/src/-private/cell-component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type {
ComponentReturn,
FlattenBlockParams,
Invoke,
} from '@glint/template/-private/integration';
import type {
ComponentSignatureArgs,
ComponentSignatureBlocks,
ComponentSignatureElement,
InvokableArgs,
} from '@glint/template/-private/signature';

/**
* A component rendered as a cell, invoked the way `ComponentLike` is.
*
* `[Invoke]` is a method here, where `ComponentLike` declares it as a property.
* TypeScript compares the parameters of a method bivariantly, so a column whose
* Cell asks for args fits a `Column<DataType>` that names none, while the args
* are still checked where the Cell is rendered.
*
* With a property, those two cannot both hold: the args of a property function
* are contravariant, so the only way to make such a column fit is `any`, which
* stops the Cell being checked at all.
*/
export type CellComponent<S = unknown> = abstract new (...args: any) => {
[Invoke](
...args: InvokableArgs<ComponentSignatureArgs<S>>
): ComponentReturn<
FlattenBlockParams<ComponentSignatureBlocks<S>>,
ComponentSignatureElement<S>
>;
};
7 changes: 4 additions & 3 deletions table/src/-private/column.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { action, get } from '@ember/object';
import { isEmpty } from '@ember/utils';

import type { CellComponent } from './cell-component.ts';
import type { Row } from './row';
import type { Table } from './table';
import type { ComponentLike, ContentValue } from '@glint/template';
Expand All @@ -24,13 +25,13 @@ export class Column<
T = unknown,
ColumnMeta = unknown,
Meta = unknown,
CellArgs = any,
CellArgs = unknown,
> {
get Cell():
| ComponentLike<CellContext<T, unknown, any> & CellArgs>
| CellComponent<CellContext<T, unknown, any> & CellArgs>
| undefined {
return this.config.Cell as
| ComponentLike<CellContext<T, unknown, any> & CellArgs>
| CellComponent<CellContext<T, unknown, any> & CellArgs>
| undefined;
}

Expand Down
4 changes: 2 additions & 2 deletions table/src/-private/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export interface Table<
DataType = unknown,
ColumnMeta = unknown,
Meta = unknown,
CellArgs = any,
CellArgs = unknown,
> {
/**
* @private
Expand Down Expand Up @@ -87,7 +87,7 @@ export class Table<
DataType = unknown,
ColumnMeta = unknown,
Meta = unknown,
CellArgs = any,
CellArgs = unknown,
> {
/**
* @private
Expand Down
22 changes: 21 additions & 1 deletion test-app/tests/integration/cells-test.gts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { setupRenderingTest } from "ember-qunit";
import { headlessTable } from "@universal-ember/table";

import type { TOC } from "@ember/component/template-only";
import type { CellContext } from "@universal-ember/table";
import type { CellContext, Column } from "@universal-ember/table";

interface Person {
name: string;
Expand Down Expand Up @@ -133,3 +133,23 @@ const TypeChecks: TOC<{ Args: { table: Context["table"] } }> = <template>
</template>;

void TypeChecks;

/**
* A column written by hand, rather than one a table inferred.
*
* Its Cell takes `@row` and `@column` and nothing else, so leaving them out is
* an error. On 4.0.0 `Cell` was `ComponentLike<CellContext<T>>` and this held.
*/
declare const handWritten: Column<Person>;

const HandWrittenColumnChecks: TOC<object> = <template>
{{#if handWritten.Cell}}
{{! @glint-expect-error a Cell is given @row and @column }}
<handWritten.Cell />

{{! @glint-expect-error a Cell is not given args it does not take }}
<handWritten.Cell @row={{undefined}} @column={{handWritten}} @nope="x" />
{{/if}}
</template>;

void HandWrittenColumnChecks;
Loading