diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..3662b370 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "typescript.tsdk": "node_modules/typescript/lib" +} \ No newline at end of file diff --git a/package.json b/package.json index 0b3f23d8..8f236ba8 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "prepare": "husky install", "prerelease": "npm run test:lint && npm run build", "release": "xs bump,publish,git-push", - "test": "vitest run", + "test": "tsc --project tsconfig.test.json && vitest run", "test:lint": "xs lint", "test:watch": "vitest" }, diff --git a/src/helpers/applyProps.ts b/src/helpers/applyProps.ts index 1389e1df..57bc00fb 100644 --- a/src/helpers/applyProps.ts +++ b/src/helpers/applyProps.ts @@ -85,7 +85,7 @@ export function applyProps( let currentInstance = instance; let targetProp = currentInstance[key]; - if ((key === 'draw') && (typeof value === 'function')) + if ((key as string === 'draw') && (typeof value === 'function')) { if (instance instanceof Graphics) { diff --git a/src/typedefs/ConstructorOptions.ts b/src/typedefs/ConstructorOptions.ts index 14993e20..441892f4 100644 --- a/src/typedefs/ConstructorOptions.ts +++ b/src/typedefs/ConstructorOptions.ts @@ -9,4 +9,5 @@ export type ConstructorOptions any> = ? unknown extends R ? ConstructorParameters[0] : R - : never; + : unknown; + diff --git a/src/typedefs/PixiElements.ts b/src/typedefs/PixiElements.ts index eb271c51..6a6e0ef4 100644 --- a/src/typedefs/PixiElements.ts +++ b/src/typedefs/PixiElements.ts @@ -4,9 +4,6 @@ import type { PixiComponents } from './PixiComponents'; import type { PixiReactElementProps } from './PixiReactNode'; export type PixiElements = { - [K in PixiComponents as K extends keyof typeof NameOverrides ? typeof NameOverrides[K] : Uncapitalize]: { - [K2 in keyof PixiReactElementProps as K2]?: PixiReactElementProps[K2] extends (...args: any) => any - ? never - : PixiReactElementProps[K2]; - }; + [K in PixiComponents as K extends keyof typeof NameOverrides ? typeof NameOverrides[K] : Uncapitalize]: + PixiReactElementProps; }; diff --git a/src/typedefs/PixiReactNode.ts b/src/typedefs/PixiReactNode.ts index 23077b6a..fae78584 100644 --- a/src/typedefs/PixiReactNode.ts +++ b/src/typedefs/PixiReactNode.ts @@ -1,3 +1,7 @@ +import { type PixiToReactEventPropNames } from '../constants/EventPropNames'; +import { type ConstructorOptions } from './ConstructorOptions'; +import { type ExcludeFunctionProps, type OmitKeys } from './UtilityTypes'; + import type { Container, Graphics, @@ -6,8 +10,6 @@ import type { Key, Ref, } from 'react'; -import type { PixiToReactEventPropNames } from '../constants/EventPropNames'; -import type { ConstructorOptions } from './ConstructorOptions'; import type { DrawCallback } from './DrawCallback'; import type { EventHandlers } from './EventHandlers'; import type { InstanceState } from './InstanceState'; @@ -15,16 +17,16 @@ import type { PixiReactChildNode } from './PixiReactChildNode'; export interface BaseNodeProps any = typeof Container> { - children: T extends Container + children?: T extends Container ? PixiReactChildNode : never; - draw?: T extends Graphics - ? DrawCallback - : null; key?: Key; ref?: Ref; } +export type GraphicsProps = T extends Graphics ? + { draw: DrawCallback } : unknown; + export interface NodeProps any = typeof Container> extends BaseNodeProps { __pixireact: InstanceState, @@ -42,17 +44,6 @@ export type PixiReactNode any = typeof Container export type PixiReactElementProps any = typeof Container> = BaseNodeProps> - & EventHandlers - & { - [ - K in keyof ConstructorOptions as ( - K extends keyof typeof PixiToReactEventPropNames - ? never - : K extends keyof NodeProps> - ? ConstructorOptions[K] extends NodeProps>[K] - ? never - : K - : K - ) - ]: ConstructorOptions[K]; - }; + & GraphicsProps> + & OmitKeys>, NodeProps & typeof PixiToReactEventPropNames> + & EventHandlers; diff --git a/src/typedefs/UtilityTypes.ts b/src/typedefs/UtilityTypes.ts new file mode 100644 index 00000000..5bb107f1 --- /dev/null +++ b/src/typedefs/UtilityTypes.ts @@ -0,0 +1,7 @@ +export type ExcludeFunctionProps = { + [K in keyof T as T[K] extends (...args: any[]) => any ? never : K]: T[K]; +}; + +export type OmitKeys = { + [K in keyof T1 as K extends keyof T2 ? never : K]: T1[K]; +}; diff --git a/test/tsconfig.json b/test/tsconfig.json new file mode 100644 index 00000000..4ff0a8e2 --- /dev/null +++ b/test/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "../tsconfig.test.json", +} \ No newline at end of file diff --git a/test/unit/typedefs/PixiElements.test.tsx b/test/unit/typedefs/PixiElements.test.tsx new file mode 100644 index 00000000..fec13d7d --- /dev/null +++ b/test/unit/typedefs/PixiElements.test.tsx @@ -0,0 +1,35 @@ +import { AlphaFilter, Container, Graphics, Sprite, Text, Texture } from 'pixi.js'; +import { describe, expect, it } from 'vitest'; +import { extend } from '../../../src'; + +extend({ Container, Graphics, Sprite, Text, Texture, AlphaFilter }); + +describe('PixiElements', () => +{ + it('applies children and props', () => + { + const elements = ( + + { /* noop */ }} /> + + + + + ); + + expect(elements.props.children).to.have.length(4); + + expect(elements.props.children[0].type).to.equal('graphics'); + expect(elements.props.children[0].props.draw).to.be.a('function'); + + expect(elements.props.children[1].type).to.equal('sprite'); + expect(elements.props.children[1].props.draw).to.be.undefined; + expect(elements.props.children[1].props.texture).to.be.instanceOf(Texture); + + expect(elements.props.children[2].type).to.equal('alphaFilter'); + expect(elements.props.children[2].props.alpha).to.equal(0.5); + + expect(elements.props.children[3].type).to.equal('pixiText'); + expect(elements.props.children[3].props.text).to.equal('Hello, World!'); + }); +}); diff --git a/tsconfig.eslint.json b/tsconfig.eslint.json index f3f0a798..e5818db1 100644 --- a/tsconfig.eslint.json +++ b/tsconfig.eslint.json @@ -5,6 +5,7 @@ "release.config.js", ], "include": [ + "./lib/**/*", "./test/**/*", "./vitest.setup.ts", "./vitest.workspace.ts" diff --git a/tsconfig.json b/tsconfig.json index 0673a6f4..571d2b0c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -28,7 +28,7 @@ "exclude": [ "dist", "node_modules", - "types" + "types", ], "include": [ "./src/**/*", diff --git a/tsconfig.test.json b/tsconfig.test.json new file mode 100644 index 00000000..442baec5 --- /dev/null +++ b/tsconfig.test.json @@ -0,0 +1,38 @@ + +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "allowJs": true, + "allowImportingTsExtensions": true, + "allowSyntheticDefaultImports": true, + "baseUrl": "./src", + "checkJs": true, + "noEmit": true, + "emitDeclarationOnly": false, + "esModuleInterop": true, + "jsx": "react-jsx", + "lib": [ + "DOM", + "ESNext" + ], + "module": "ESNext", + "moduleResolution": "node", + "noUnusedLocals": true, + "noUnusedParameters": true, + "pretty": true, + "resolveJsonModule": true, + "skipLibCheck": true, + "sourceMap": true, + "strict": true, + "target": "ESNext" + }, + "exclude": [ + "dist", + "node_modules", + "types" + ], + "include": [ + "./src/**/*", + "./test/**/*", + ] +} diff --git a/vitest.workspace.ts b/vitest.workspace.ts index 736729bb..090e991e 100644 --- a/vitest.workspace.ts +++ b/vitest.workspace.ts @@ -4,7 +4,7 @@ export default defineWorkspace([ { test: { environment: 'jsdom', - include: ['test/unit/**/*.test.ts'], + include: ['test/unit/**/*.test.ts?(x)'], pool: 'forks', }, },