Skip to content

Commit

Permalink
Stricter typescript type for Trans components prop (#1516)
Browse files Browse the repository at this point in the history
* Example of how types allow incorrect usage

* Add correct types and allow tslint to run on tsx files

* Remove invalid type example

* Remove test for invalid behaviour
  • Loading branch information
danreeves authored Jun 10, 2022
1 parent b2dcdfa commit 5c80355
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface TransProps<E extends Element = HTMLDivElement>
extends React.HTMLProps<E>,
Partial<WithT> {
children?: React.ReactNode;
components?: readonly React.ReactNode[] | { readonly [tagName: string]: React.ReactNode };
components?: readonly React.ReactElement[] | { readonly [tagName: string]: React.ReactElement };
count?: number;
context?: string;
defaults?: string;
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@
"test:watch": "cross-env BABEL_ENV=development jest --no-cache --watch",
"test:coverage": "cross-env BABEL_ENV=development jest --no-cache --coverage",
"test:lint": "eslint ./src ./test",
"test:typescript": "tslint --project tsconfig.json '**/*.ts'",
"test:typescript:noninterop": "tslint --project tsconfig.nonEsModuleInterop.json '**/*.ts'",
"test:typescript:customtypes": "tslint --project ./test/typescript/custom-types/tsconfig.json '**/*.ts'",
"test:typescript": "tslint --project tsconfig.json '**/*.{ts,tsx}'",
"test:typescript:noninterop": "tslint --project tsconfig.nonEsModuleInterop.json '**/*.{ts,tsx}'",
"test:typescript:customtypes": "tslint --project ./test/typescript/custom-types/tsconfig.json '**/*.{ts,tsx}'",
"contributors:add": "all-contributors add",
"contributors:generate": "all-contributors generate",
"prettier": "prettier --write \"{,**/}*.{ts,tsx,js,json,md}\""
Expand Down
8 changes: 8 additions & 0 deletions test/typescript/Trans.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ function objectComponents() {
return <Trans components={{ Btn: <button /> }} defaults="Hello <Btn />" />;
}

function MyComponent() {
return <>world</>;
}

function objectCustomComponents() {
return <Trans components={{ Btn: <MyComponent /> }} defaults="Hello <Btn />" />;
}

function constObjectComponents() {
const constObject = {
Btn: <button />,
Expand Down
2 changes: 1 addition & 1 deletion ts4.1/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export type TransProps<
E = React.HTMLProps<HTMLDivElement>
> = E & {
children?: TransChild | TransChild[];
components?: readonly React.ReactNode[] | { readonly [tagName: string]: React.ReactNode };
components?: readonly React.ReactElement[] | { readonly [tagName: string]: React.ReactElement };
count?: number;
context?: string;
defaults?: string;
Expand Down

0 comments on commit 5c80355

Please sign in to comment.