diff --git a/src/writer/writer-ts-definitions.ts b/src/writer/writer-ts-definitions.ts index bbd0393..2f657e3 100644 --- a/src/writer/writer-ts-definitions.ts +++ b/src/writer/writer-ts-definitions.ts @@ -90,13 +90,13 @@ function genPropDef(def: Pick & $Props; + export type ${props_name}${genericsName} = Omit & $Props${genericsName}; `; } else { prop_def = ` diff --git a/tests/__snapshots__/fixtures.test.ts.snap b/tests/__snapshots__/fixtures.test.ts.snap index 7a1b411..326b50c 100644 --- a/tests/__snapshots__/fixtures.test.ts.snap +++ b/tests/__snapshots__/fixtures.test.ts.snap @@ -483,6 +483,69 @@ exports[`fixtures (JSON) "typedefs/input.svelte" 1`] = ` }" `; +exports[`fixtures (JSON) "generics-with-rest-props/input.svelte" 1`] = ` +"{ + "props": [ + { + "name": "headers", + "kind": "let", + "type": "ReadonlyArray>", + "value": "[]", + "isFunction": false, + "isFunctionDeclaration": false, + "isRequired": false, + "constant": false, + "reactive": false + }, + { + "name": "rows", + "kind": "let", + "type": "ReadonlyArray", + "value": "[]", + "isFunction": false, + "isFunctionDeclaration": false, + "isRequired": false, + "constant": false, + "reactive": false + } + ], + "moduleExports": [], + "slots": [ + { + "name": "__default__", + "default": true, + "slot_props": "{ headers: ReadonlyArray>, rows: ReadonlyArray }" + } + ], + "events": [], + "typedefs": [ + { + "type": "{ id: string | number; [key: string]: any; }", + "name": "DataTableRow", + "ts": "interface DataTableRow { id: string | number; [key: string]: any; }" + }, + { + "type": "Exclude", + "name": "DataTableKey", + "ts": "type DataTableKey = Exclude" + }, + { + "type": "{ key: DataTableKey; value: string; }", + "name": "DataTableHeader", + "ts": "interface DataTableHeader { key: DataTableKey; value: string; }" + } + ], + "generics": [ + "Row", + "Row extends DataTableRow = DataTableRow" + ], + "rest_props": { + "type": "Element", + "name": "div" + } +}" +`; + exports[`fixtures (JSON) "bind-this/input.svelte" 1`] = ` "{ "props": [ @@ -1518,6 +1581,48 @@ export default class Typedefs extends SvelteComponentTyped< " `; +exports[`fixtures (TypeScript) "generics-with-rest-props/input.svelte" 1`] = ` +"import type { SvelteComponentTyped } from "svelte"; +import type { SvelteHTMLElements } from "svelte/elements"; + +export interface DataTableRow { + id: string | number; + [key: string]: any; +} + +export type DataTableKey = Exclude; + +export interface DataTableHeader { + key: DataTableKey; + value: string; +} + +type RestProps = SvelteHTMLElements["div"]; + +type $Props = { + /** + * @default [] + */ + headers?: ReadonlyArray>; + + /** + * @default [] + */ + rows?: ReadonlyArray; + + [key: \`data-${string}\`]: any; +}; + +export type GenericsWithRestPropsProps = Omit> & $Props; + +export default class GenericsWithRestProps extends SvelteComponentTyped< + GenericsWithRestPropsProps, + Record, + { default: { headers: ReadonlyArray>; rows: ReadonlyArray } } +> {} +" +`; + exports[`fixtures (TypeScript) "bind-this/input.svelte" 1`] = ` "import type { SvelteComponentTyped } from "svelte"; diff --git a/tests/e2e/carbon/types/DataTable/DataTable.svelte.d.ts b/tests/e2e/carbon/types/DataTable/DataTable.svelte.d.ts index a4930de..0caae64 100644 --- a/tests/e2e/carbon/types/DataTable/DataTable.svelte.d.ts +++ b/tests/e2e/carbon/types/DataTable/DataTable.svelte.d.ts @@ -44,7 +44,7 @@ export interface DataTableCell { type RestProps = SvelteHTMLElements["div"]; -type $Props = { +type $Props = { /** * Specify the data table headers * @default [] @@ -183,7 +183,8 @@ type $Props = { [key: `data-${string}`]: any; }; -export type DataTableProps = Omit & $Props; +export type DataTableProps = Omit> & + $Props; export default class DataTable< Row extends DataTableRow = DataTableRow diff --git a/tests/fixtures/generics-with-rest-props/Test.svelte b/tests/fixtures/generics-with-rest-props/Test.svelte new file mode 100644 index 0000000..9145677 --- /dev/null +++ b/tests/fixtures/generics-with-rest-props/Test.svelte @@ -0,0 +1,50 @@ + + + + {#each rows as row} + {row.name} + {row.port} + {/each} + + + + {#each rows as row} + {row.name} + {/each} + diff --git a/tests/fixtures/generics-with-rest-props/input.svelte b/tests/fixtures/generics-with-rest-props/input.svelte new file mode 100644 index 0000000..85ab904 --- /dev/null +++ b/tests/fixtures/generics-with-rest-props/input.svelte @@ -0,0 +1,19 @@ + + +
+ +
diff --git a/tests/fixtures/generics-with-rest-props/output.d.ts b/tests/fixtures/generics-with-rest-props/output.d.ts new file mode 100644 index 0000000..835db65 --- /dev/null +++ b/tests/fixtures/generics-with-rest-props/output.d.ts @@ -0,0 +1,38 @@ +import type { SvelteComponentTyped } from "svelte"; +import type { SvelteHTMLElements } from "svelte/elements"; + +export interface DataTableRow { + id: string | number; + [key: string]: any; +} + +export type DataTableKey = Exclude; + +export interface DataTableHeader { + key: DataTableKey; + value: string; +} + +type RestProps = SvelteHTMLElements["div"]; + +type $Props = { + /** + * @default [] + */ + headers?: ReadonlyArray>; + + /** + * @default [] + */ + rows?: ReadonlyArray; + + [key: `data-${string}`]: any; +}; + +export type GenericsWithRestPropsProps = Omit> & $Props; + +export default class GenericsWithRestProps extends SvelteComponentTyped< + GenericsWithRestPropsProps, + Record, + { default: { headers: ReadonlyArray>; rows: ReadonlyArray } } +> {} diff --git a/tests/fixtures/generics-with-rest-props/output.json b/tests/fixtures/generics-with-rest-props/output.json new file mode 100644 index 0000000..71f7f0c --- /dev/null +++ b/tests/fixtures/generics-with-rest-props/output.json @@ -0,0 +1,60 @@ +{ + "props": [ + { + "name": "headers", + "kind": "let", + "type": "ReadonlyArray>", + "value": "[]", + "isFunction": false, + "isFunctionDeclaration": false, + "isRequired": false, + "constant": false, + "reactive": false + }, + { + "name": "rows", + "kind": "let", + "type": "ReadonlyArray", + "value": "[]", + "isFunction": false, + "isFunctionDeclaration": false, + "isRequired": false, + "constant": false, + "reactive": false + } + ], + "moduleExports": [], + "slots": [ + { + "name": "__default__", + "default": true, + "slot_props": "{ headers: ReadonlyArray>, rows: ReadonlyArray }" + } + ], + "events": [], + "typedefs": [ + { + "type": "{ id: string | number; [key: string]: any; }", + "name": "DataTableRow", + "ts": "interface DataTableRow { id: string | number; [key: string]: any; }" + }, + { + "type": "Exclude", + "name": "DataTableKey", + "ts": "type DataTableKey = Exclude" + }, + { + "type": "{ key: DataTableKey; value: string; }", + "name": "DataTableHeader", + "ts": "interface DataTableHeader { key: DataTableKey; value: string; }" + } + ], + "generics": [ + "Row", + "Row extends DataTableRow = DataTableRow" + ], + "rest_props": { + "type": "Element", + "name": "div" + } +} \ No newline at end of file