Skip to content

Commit 852f84e

Browse files
committed
type: add more complete type inference.
1 parent 257eecb commit 852f84e

30 files changed

+42
-36
lines changed

core/src/section/Copied.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const example = {
1111

1212
it('renders <JsonView /> Copied test case', async () => {
1313
const user = userEvent.setup();
14-
1514
// Mock the necessary functions and values
1615
const writeTextMock = jest.fn().mockResolvedValue(undefined);
1716
jest.spyOn(navigator.clipboard, 'writeText').mockImplementation(writeTextMock);
@@ -87,6 +86,7 @@ it('renders <JsonView.Copied /> render test case', async () => {
8786
const { container } = render(
8887
<JsonView value={{ value: 123 }}>
8988
<JsonView.Copied
89+
as="span"
9090
data-testid="copied"
9191
render={(props) => {
9292
return <span {...props}>xx</span>;

core/src/section/Copied.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { type TagType } from '../store/Types';
22
import { type SectionElement, useSectionStore } from '../store/Section';
33
import { useSectionRender } from '../utils/useRender';
44

5-
export const Copied = (props: SectionElement<TagType>) => {
5+
export const Copied = <K extends TagType = 'svg'>(props: SectionElement<K>) => {
66
const { Copied: Comp = {} } = useSectionStore();
77
useSectionRender(Comp, props, 'Copied');
88
return null;

core/src/section/CountInfo.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export interface CountInfoCompProps<T extends object> {
1616
keyName: string | number;
1717
}
1818

19-
export const CountInfoComp = <T extends object>(
20-
props: SectionElementProps<TagType> & CountInfoCompProps<T> & React.HTMLAttributes<HTMLElement>,
19+
export const CountInfoComp = <K extends TagType, T extends object>(
20+
props: SectionElementProps<K> & CountInfoCompProps<T> & React.HTMLAttributes<HTMLElement>,
2121
) => {
2222
const { value = {}, keyName, ...other } = props;
2323
const { displayObjectSize } = useStore();
@@ -38,7 +38,7 @@ export const CountInfoComp = <T extends object>(
3838

3939
const elmProps = { ...reset, ...other };
4040
const isRender = render && typeof render === 'function';
41-
const child = isRender && render({ ...elmProps, 'data-length': len }, { value, keyName });
41+
const child = isRender && render({ ...elmProps, 'data-length': len } as React.HTMLAttributes<K>, { value, keyName });
4242
if (child) return child;
4343
return <Elm {...elmProps} />;
4444
};

core/src/section/CountInfoExtra.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export interface CountInfoExtraCompsProps<T extends object> {
1515
keyName: string | number;
1616
}
1717

18-
export const CountInfoExtraComps = <T extends object>(
19-
props: SectionElementProps<TagType> & CountInfoExtraCompsProps<T>,
18+
export const CountInfoExtraComps = <T extends object, K extends TagType>(
19+
props: SectionElementProps<K> & CountInfoExtraCompsProps<T>,
2020
) => {
2121
const { value = {}, keyName, ...other } = props;
2222
const { CountInfoExtra: Comp = {} } = useSectionStore();
@@ -25,7 +25,7 @@ export const CountInfoExtraComps = <T extends object>(
2525
const Elm = as || 'span';
2626
const isRender = render && typeof render === 'function';
2727
const elmProps = { ...reset, ...other };
28-
const child = isRender && render(elmProps, { value, keyName });
28+
const child = isRender && render(elmProps as React.HTMLAttributes<K>, { value, keyName });
2929
if (child) return child;
3030
return <Elm {...elmProps} />;
3131
};

core/src/store/Section.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React, { FC, PropsWithChildren, ComponentPropsWithoutRef, createContext, useContext, useReducer } from 'react';
22
import { type TagType } from './Types';
33

4-
export type SectionElementProps<T extends TagType> = {
4+
export type SectionElementProps<T extends TagType = 'span'> = {
55
as?: T;
66
render?: (props: SectionElement<T>, result: { value: unknown; keyName: string | number }) => React.ReactNode;
77
};
88

9-
export type SectionElement<T extends TagType> = SectionElementProps<T> & ComponentPropsWithoutRef<T>;
9+
export type SectionElement<T extends TagType = 'span'> = SectionElementProps<T> & ComponentPropsWithoutRef<T>;
1010

1111
type InitialState<T extends TagType> = {
1212
Copied?: SectionElement<T>;

core/src/store/Symbols.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import {
1010
import { type TagType } from './Types';
1111
import { TriangleArrow } from '../arrow/TriangleArrow';
1212

13-
type SymbolsElementProps<T extends TagType> = {
13+
type SymbolsElementProps<T extends TagType = 'span'> = {
1414
as?: T;
1515
render?: (props: SymbolsElement<T>) => React.ReactNode;
1616
'data-type'?: string;
1717
};
18-
export type SymbolsElement<T extends TagType> = SymbolsElementProps<T> & ComponentPropsWithoutRef<T>;
18+
export type SymbolsElement<T extends TagType = 'span'> = SymbolsElementProps<T> & ComponentPropsWithoutRef<T>;
1919

2020
type InitialState<T extends ElementType = 'span'> = {
2121
Arrow?: SymbolsElement<T>;

core/src/symbol/Arrow.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useSymbolsStore, type SymbolsElement } from '../store/Symbols';
22
import { type TagType } from '../store/Types';
33
import { useSymbolsRender } from '../utils/useRender';
44

5-
export const Arrow = (props: SymbolsElement<TagType>) => {
5+
export const Arrow = <K extends TagType = 'span'>(props: SymbolsElement<K>) => {
66
const { Arrow: Comp = {} } = useSymbolsStore();
77
useSymbolsRender(Comp, props, 'Arrow');
88
return null;

core/src/symbol/BraceLeft.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useSymbolsStore, type SymbolsElement } from '../store/Symbols';
22
import { type TagType } from '../store/Types';
33
import { useSymbolsRender } from '../utils/useRender';
44

5-
export const BraceLeft = (props: SymbolsElement<TagType>) => {
5+
export const BraceLeft = <K extends TagType = 'span'>(props: SymbolsElement<K>) => {
66
const { BraceLeft: Comp = {} } = useSymbolsStore();
77
useSymbolsRender(Comp, props, 'BraceLeft');
88

core/src/symbol/BraceRight.test.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ it('renders <JsonView.BraceRight /> test case', async () => {
77
};
88
const { container } = render(
99
<JsonView value={demo}>
10-
<JsonView.BraceRight data-testid="brace">x</JsonView.BraceRight>
10+
<JsonView.BraceRight as="span" data-testid="brace">
11+
x
12+
</JsonView.BraceRight>
1113
</JsonView>,
1214
);
1315
expect(container.firstElementChild).toBeInstanceOf(Element);

core/src/symbol/BraceRight.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useSymbolsStore, type SymbolsElement } from '../store/Symbols';
22
import { type TagType } from '../store/Types';
33
import { useSymbolsRender } from '../utils/useRender';
44

5-
export const BraceRight = (props: SymbolsElement<TagType>) => {
5+
export const BraceRight = <K extends TagType = 'span'>(props: SymbolsElement<K>) => {
66
const { BraceRight: Comp = {} } = useSymbolsStore();
77
useSymbolsRender(Comp, props, 'BraceRight');
88
return null;

core/src/symbol/BracketsLeft.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useSymbolsStore, type SymbolsElement } from '../store/Symbols';
22
import { type TagType } from '../store/Types';
33
import { useSymbolsRender } from '../utils/useRender';
44

5-
export const BracketsLeft = (props: SymbolsElement<TagType>) => {
5+
export const BracketsLeft = <K extends TagType = 'span'>(props: SymbolsElement<K>) => {
66
const { BracketsLeft: Comp = {} } = useSymbolsStore();
77
useSymbolsRender(Comp, props, 'BracketsLeft');
88

core/src/symbol/BracketsRight.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useSymbolsStore, type SymbolsElement } from '../store/Symbols';
22
import { type TagType } from '../store/Types';
33
import { useSymbolsRender } from '../utils/useRender';
44

5-
export const BracketsRight = (props: SymbolsElement<TagType>) => {
5+
export const BracketsRight = <K extends TagType = 'span'>(props: SymbolsElement<K>) => {
66
const { BracketsRight: Comp = {} } = useSymbolsStore();
77
useSymbolsRender(Comp, props, 'BracketsRight');
88

core/src/symbol/Colon.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useSymbolsStore, type SymbolsElement } from '../store/Symbols';
22
import { type TagType } from '../store/Types';
33
import { useSymbolsRender } from '../utils/useRender';
44

5-
export const Colon = (props: SymbolsElement<TagType>) => {
5+
export const Colon = <K extends TagType = 'span'>(props: SymbolsElement<K>) => {
66
const { Colon: Comp = {} } = useSymbolsStore();
77
useSymbolsRender(Comp, props, 'Colon');
88

core/src/symbol/Quote.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useSymbolsStore, type SymbolsElement } from '../store/Symbols';
22
import { type TagType } from '../store/Types';
33
import { useSymbolsRender } from '../utils/useRender';
44

5-
export const Quote = (props: SymbolsElement<TagType>) => {
5+
export const Quote = <K extends TagType = 'span'>(props: SymbolsElement<K>) => {
66
const { Quote: Comp = {} } = useSymbolsStore();
77
useSymbolsRender(Comp, props, 'Quote');
88

core/src/symbol/ValueQuote.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useSymbolsStore, type SymbolsElement } from '../store/Symbols';
22
import { type TagType } from '../store/Types';
33
import { useSymbolsRender } from '../utils/useRender';
44

5-
export const ValueQuote = (props: SymbolsElement<TagType>) => {
5+
export const ValueQuote = <K extends TagType = 'span'>(props: SymbolsElement<K>) => {
66
const { ValueQuote: Comp = {} } = useSymbolsStore();
77
useSymbolsRender(Comp, props, 'ValueQuote');
88

core/src/types/Bigint.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useTypesStore, type TagType, type TypesElement } from '../store/Types';
22
import { useTypesRender } from '../utils/useRender';
33

4-
export const Bigint = <K extends TagType>(props: TypesElement<K>) => {
4+
export const Bigint = <K extends TagType = 'span'>(props: TypesElement<K>) => {
55
const { Bigint: Comp = {} } = useTypesStore();
66
useTypesRender(Comp, props, 'Bigint');
77

core/src/types/Date.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useTypesStore, type TagType, type TypesElement } from '../store/Types';
22
import { useTypesRender } from '../utils/useRender';
33

4-
export const Date = (props: TypesElement<TagType>) => {
4+
export const Date = <K extends TagType = 'span'>(props: TypesElement<K>) => {
55
const { Date: Comp = {} } = useTypesStore();
66
useTypesRender(Comp, props, 'Date');
77

core/src/types/False.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useTypesStore, type TagType, type TypesElement } from '../store/Types';
22
import { useTypesRender } from '../utils/useRender';
33

4-
export const False = (props: TypesElement<TagType>) => {
4+
export const False = <K extends TagType = 'span'>(props: TypesElement<K>) => {
55
const { False: Comp = {} } = useTypesStore();
66
useTypesRender(Comp, props, 'False');
77

core/src/types/Float.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useTypesStore, type TagType, type TypesElement } from '../store/Types';
22
import { useTypesRender } from '../utils/useRender';
33

4-
export const Float = (props: TypesElement<TagType>) => {
4+
export const Float = <K extends TagType = 'span'>(props: TypesElement<K>) => {
55
const { Float: Comp = {} } = useTypesStore();
66
useTypesRender(Comp, props, 'Float');
77

core/src/types/Int.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useTypesStore, type TagType, type TypesElement } from '../store/Types';
22
import { useTypesRender } from '../utils/useRender';
33

4-
export const Int = (props: TypesElement<TagType>) => {
4+
export const Int = <K extends TagType = 'span'>(props: TypesElement<K>) => {
55
const { Int: Comp = {} } = useTypesStore();
66
useTypesRender(Comp, props, 'Int');
77

core/src/types/Map.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useTypesStore, type TagType, type TypesElement } from '../store/Types';
22
import { useTypesRender } from '../utils/useRender';
33

4-
export const Map = (props: TypesElement<TagType>) => {
4+
export const Map = <K extends TagType = 'span'>(props: TypesElement<K>) => {
55
const { Map: Comp = {} } = useTypesStore();
66
useTypesRender(Comp, props, 'Map');
77

core/src/types/Nan.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useTypesStore, type TagType, type TypesElement } from '../store/Types';
22
import { useTypesRender } from '../utils/useRender';
33

4-
export const Nan = (props: TypesElement<TagType>) => {
4+
export const Nan = <K extends TagType = 'span'>(props: TypesElement<K>) => {
55
const { Nan: Comp = {} } = useTypesStore();
66
useTypesRender(Comp, props, 'Nan');
77

core/src/types/Null.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useTypesStore, type TagType, type TypesElement } from '../store/Types';
22
import { useTypesRender } from '../utils/useRender';
33

4-
export const Null = (props: TypesElement<TagType>) => {
4+
export const Null = <K extends TagType = 'span'>(props: TypesElement<K>) => {
55
const { Null: Comp = {} } = useTypesStore();
66
useTypesRender(Comp, props, 'Null');
77

core/src/types/Set.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useTypesStore, type TagType, type TypesElement } from '../store/Types';
22
import { useTypesRender } from '../utils/useRender';
33

4-
export const Set = (props: TypesElement<TagType>) => {
4+
export const Set = <K extends TagType = 'span'>(props: TypesElement<K>) => {
55
const { Set: Comp = {} } = useTypesStore();
66
useTypesRender(Comp, props, 'Set');
77

core/src/types/String.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import userEvent from '@testing-library/user-event';
2-
import { screen, render, waitFor } from '@testing-library/react';
2+
import { screen, render } from '@testing-library/react';
33
import JsonView from '..';
44

55
it('renders <JsonView.String /> test case', async () => {

core/src/types/String.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useTypesStore, type TagType, type TypesElement } from '../store/Types';
22
import { useTypesRender } from '../utils/useRender';
33

4-
export const StringText = (props: TypesElement<TagType>) => {
4+
export const StringText = <K extends TagType = 'span'>(props: TypesElement<K>) => {
55
const { Str: Comp = {} } = useTypesStore();
66
useTypesRender(Comp, props, 'Str');
77

core/src/types/True.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useTypesStore, type TagType, type TypesElement } from '../store/Types';
22
import { useTypesRender } from '../utils/useRender';
33

4-
export const True = (props: TypesElement<TagType>) => {
4+
export const True = <K extends TagType = 'span'>(props: TypesElement<K>) => {
55
const { True: Comp = {} } = useTypesStore();
66
useTypesRender(Comp, props, 'True');
77

core/src/types/Undefined.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useTypesStore, type TagType, type TypesElement } from '../store/Types';
22
import { useTypesRender } from '../utils/useRender';
33

4-
export const Undefined = (props: TypesElement<TagType>) => {
4+
export const Undefined = <K extends TagType = 'span'>(props: TypesElement<K>) => {
55
const { Undefined: Comp = {} } = useTypesStore();
66
useTypesRender(Comp, props, 'Undefined');
77

core/src/types/Url.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useTypesStore, type TagType, type TypesElement } from '../store/Types';
22
import { useTypesRender } from '../utils/useRender';
33

4-
export const Url = (props: TypesElement<TagType>) => {
4+
export const Url = <K extends TagType = 'a'>(props: TypesElement<K>) => {
55
const { Url: Comp = {} } = useTypesStore();
66
useTypesRender(Comp, props, 'Url');
77

core/src/utils/useRender.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { useSymbolsDispatch, type SymbolsElement } from '../store/Symbols';
33
import { useTypesDispatch, type TagType, type TypesElement } from '../store/Types';
44
import { useSectionDispatch, type SectionElement } from '../store/Section';
55

6-
export function useSymbolsRender(currentProps: SymbolsElement<TagType>, props: SymbolsElement<TagType>, key: string) {
6+
export function useSymbolsRender<K extends TagType>(
7+
currentProps: SymbolsElement<TagType>,
8+
props: SymbolsElement<K>,
9+
key: string,
10+
) {
711
const dispatch = useSymbolsDispatch();
812
const cls = [currentProps.className, props.className].filter(Boolean).join(' ');
913
const reset = {

0 commit comments

Comments
 (0)