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

DataTable: The parameter of the function passed to sortIcon has the wrong type #4639

Closed
Suppen opened this issue Jul 11, 2023 · 8 comments · Fixed by #4642
Closed

DataTable: The parameter of the function passed to sortIcon has the wrong type #4639

Suppen opened this issue Jul 11, 2023 · 8 comments · Fixed by #4642
Assignees
Labels
Typescript Issue or pull request is *only* related to TypeScript definition
Milestone

Comments

@Suppen
Copy link

Suppen commented Jul 11, 2023

Describe the bug

When using a function for the sortIcon prop of a <DataTable>, the function's first parameter, options, is of type IconOptions<DataTable<TValue>>. This type knows about icons, but not about sorting.

Logging the options parameter to console shows it has more props than its type claims. The most important one is sortOrder, telling which direction a column is sorted. TS does not know about this, so options.sortOrder defaults to type any. It should be 0 | 1 | -1

In addition to sortOrder, there is also a property sorted which the type does not know about. Its actual type appears to for some reason be null | true, instead of boolean.

Reproducer

https://stackblitz.com/edit/vitejs-vite-s3jiwf?file=src%2FApp.tsx

PrimeReact version

9.6.0

React version

18.x

Language

TypeScript

Build / Runtime

Create React App (CRA)

Browser(s)

No response

Steps to reproduce the behavior

Hover options.sortOrder and options.sorted in the reproducer. They both show type any.

Check the console, where both props are logged. They clearly have values.

Expected behavior

The type should know about options.sortOrder, which should have type 0 | 1 | -1.

In addition, the type should know about options.sorted , which for some reason should have type null | true

@Suppen Suppen added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Jul 11, 2023
EmirBoyaci added a commit to EmirBoyaci/primereact that referenced this issue Jul 11, 2023
@EmirBoyaci
Copy link
Contributor

After my wrong PR (🤣), I found that these sortOrder and sorted properties should be exposed into IconOptions interface. We may have define 2nd generic parameter to expose these two properties into IconOptions.

export interface IconOptions<ParentProps> {
iconProps: React.HTMLProps<HTMLElement>;
element: React.ReactNode;
props?: ParentProps;
[key: string]: any;
}

@melloware
Copy link
Member

I can fix it. Give me 5 minutes.

@melloware melloware added Typescript Issue or pull request is *only* related to TypeScript definition and removed Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible labels Jul 11, 2023
@melloware melloware self-assigned this Jul 11, 2023
@melloware melloware added this to the 9.6.1 milestone Jul 11, 2023
@melloware
Copy link
Member

OK not as easy as I thought.

if I do...

sortIcon?: IconType<IconOptions<DataTable<TValue>> & { sorted?: boolean; sortOrder?: number }> | undefined;

I can see the options has this:

image

But it is still showing as any when I hover sorted

@EmirBoyaci
Copy link
Contributor

OK not as easy as I thought.

if I do...

sortIcon?: IconType<IconOptions<DataTable<TValue>> & { sorted?: boolean; sortOrder?: number }> | undefined;

I can see the options has this:

image

But it is still showing as any when I hover sorted

This one was my first attempt but didn't worked. Because when I look at the IconOptions, I saw the options is type IconOptions<ParentType> and, these two additional properties sorted and sortOrder have to be defined in IconOptions.

See:

export type IconType<ParentProps> = React.ReactNode | ((options: IconOptions<ParentProps>) => React.ReactNode);

@melloware
Copy link
Member

Yep but...

export interface IconOptions<ParentProps> {
    iconProps: React.HTMLProps<HTMLElement>;
    element: React.ReactNode;
    props?: ParentProps;
    [key: string]: any;
}

The [key: string]: any; says ANY extra props can be included so I was hoping by just appending the sorted props it would be allowed because of the [key: string]: any;

@EmirBoyaci
Copy link
Contributor

I also thought exactly the same as you but didn't worked 😄 I think it is because the ParentProps is going to the props field not the IconOptions interface..

@melloware
Copy link
Member

Yep I think the generics for IconType need to be extended a little to make it more flexible but I am trying to figure out how. 😄

@EmirBoyaci
Copy link
Contributor

EmirBoyaci commented Jul 11, 2023

I have a suggestion, let me finalize and I can raise PR

EmirBoyaci added a commit to EmirBoyaci/primereact that referenced this issue Jul 11, 2023
@melloware melloware assigned EmirBoyaci and unassigned melloware Jul 11, 2023
melloware added a commit that referenced this issue Jul 11, 2023
… in base object (#4642)

* feat: expose sorted and sortOrder properties for sortIcon

Closes #4639

* feat: allow additional props from IconType to expose additional props in base object

Closes #4639

* fix: make sortOrder and sorted propesties nullable

* Update utils.d.ts

---------

Co-authored-by: Melloware <mellowaredev@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Typescript Issue or pull request is *only* related to TypeScript definition
Projects
None yet
3 participants