snovakovic / fast-sort

Blazing fast array sorting with TypeScript support.
MIT License
303 stars 28 forks source link

Typescript issue "Argument of type is not assignable" #21

Closed peps closed 4 years ago

peps commented 4 years ago

Hi, really excited to find this great library! The code is working, but my editor is giving the following Typescript error.

Argument of type '"created_at"' is not assignable to parameter of type 'ISortByFunction<unknown> | ISortByFunction<unknown>[] | ISortBy<unknown>[]'.

const entries = [
{ id: 1, name: 'Project 1', created_at: '2020-04-23T01:30:59.657Z'},
{ id: 2, name: 'Project 2', created_at: '2020-04-23T01:30:58.657Z'},
{ id: 3, name: 'Project 3', created_at: '2020-04-23T01:30:57.657Z'}
];

const projects = sort(entries).desc('created_at');

Any help would be greatly appreciated, i'm pretty new to Typescript, not sure if it's something im doing wrong, or if I found a bug. Thanks!

snovakovic commented 4 years ago

@peps I'm unable to reproduce issue with the code example you provided.

There is similar TS issue #17 so you can check that one for more info if that helps.

What version of TS are you using? So I can check with correct version. (might be that it's not working as expected in different TS versions)

For now you should be able to override the TS issue by casting argument to any e.g const projects = sort(entries).desc('created_at' as any);

peps commented 4 years ago

@snovakovic thanks for the quick response. I'm using the latest TS v3.8.3. Forcing 'created_at' as any got rid of the error. Not ideal, but everything's working :) Thanks!

snovakovic commented 4 years ago

Not ideal, but everything's working :)

@peps Yeah I agree far from ideal! I can't offer better suggestion as I currently can't replicate the issue on my end.

Could you maybe try to update to function comparer as: const projects = sort(entries).desc(entry => entry.created_at);

Does that also throws error? and if it does could you share what is the interface of the entry property?

Also if you could replicate your issue in the online editor I could then take a look at it https://stackblitz.com/edit/typescript-ygxxy6?file=index.ts

peps commented 4 years ago

const projects = sort(entries).desc(entry => entry.created_at);

gives an error for me on the 'created_at' Property 'created_at' does not exist on type 'unknown'.ts(2339) but it works if I force any onto it.

const projects = sort(entries).desc( (entry: any) => entry.created_at);

My 'entries' array is just set to 'any' right now as well, in my code it's just the response json from the database, so I didn't set up an interface for it yet, and tyepscript doesnt infer the type automatically... Come to think of it that might just be the issue, caused by my Typescript lazyness ;)

snovakovic commented 4 years ago

@peps In that case I will close this issue.

In case you can replicate issue using https://stackblitz.com/ feel free to reopen it and I will take a look into it.