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

Text components style property is not correctly typed #311

Open
zOadT opened this issue Jan 11, 2023 · 2 comments
Open

Text components style property is not correctly typed #311

zOadT opened this issue Jan 11, 2023 · 2 comments

Comments

@zOadT
Copy link
Contributor

zOadT commented Jan 11, 2023

Description

Since pixi.js 7.1 the type of Text.style is PIXI.TextStyle | Partial<PIXI.ITextStyle>. But pix-react-fiber only allows to set PIXI.TextStyle objects as style attribute (since 7.1 every property is required in PIXI.TextStyle)

Steps to reproduce

<Text
	text='Text'
	// Type '{ fontSize: number; }' is missing the following properties from type 'TextStyle': styleID, _align, _breakWords, _dropShadow, and 57 more.ts(2740)
	style={{
		fontSize: 64,
	}}
/>

Additional info

  • react-pixi-fiber version: 1.0.1
  • React version:
  • ReactDOM version:
  • PixiJS version: 7.1
@zOadT
Copy link
Contributor Author

zOadT commented Jan 11, 2023

I shortly looked into it: The problem seems to be that style has different types for the getter and setter:

// @pixi/text/lib/Text.d.ts
get style(): TextStyle;
set style(style: TextStyle | Partial<ITextStyle>);

There are Issues in the typescript repo that indicate that it is currently not possible to receive the type of the setters, so it seems like we have to manually adjust the type in our types 😟

@zOadT
Copy link
Contributor Author

zOadT commented Jan 15, 2023

In case someone else runs into this: As a workaround you can define a .d.ts file locally with the content

import type { IPointData, ITextStyle, TextStyle } from 'pixi.js'

declare module 'react-pixi-fiber' {
	interface PixiComponent<P = {}> {
		(props: Setters<PixiElement<P>>): React.ReactElement<P>;
	}

	type Override<P, E> = Omit<P, keyof E> & Partial<E>

	type Setters<P> = P extends Text
		? Override<P, {
			text: string | number;
			style: TextStyle | Partial<ITextStyle>;
		}>
		: P extends TilingSprite
			? Override<P, {
				tileScale: IPointData;
			}>
			: P;
}

Definitively not a clean solution, but I don't think we want explicit type information in the typings of this library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant