javascriptdata / danfojs

Danfo.js is an open source, JavaScript library providing high performance, intuitive, and easy to use data structures for manipulating and processing structured data.
https://danfo.jsdata.org/
MIT License
4.81k stars 209 forks source link

Typing issue with void functions #294

Closed mebibou closed 2 years ago

mebibou commented 3 years ago

Describe the bug

When using typescript and operation like drop, it cannot be chained directly to another operation or the compiler will throw an error like Property 'to_csv' does not exist on type 'void | DataFrame'.\n Property 'to_csv' does not exist on type 'void'.

To Reproduce

import { DataFrame } from 'danfojs-node';

const df = new DataFrame([]);
df.drop({ columns, inplace: false }).to_csv();

Expected behavior

It should return void only when using inplace: true. Currently it assumes it could be both, but the parameters should infer which return type it expects.

Typescript version 3.9.10 and danfojs-node version 0.3.3 used

risenW commented 3 years ago

@mebibou Have you tried this:

df.drop({ columns, inplace: false })?.to_csv();

mebibou commented 3 years ago

? is when the function can be undefined, doesn't circumvent the void. Same error

The only thing that currently works is (df.to_csv() as DataFrame). Works but not pretty

mebibou commented 3 years ago

FYI, this can be fixed this way: https://www.typescriptlang.org/docs/handbook/2/functions.html#function-overloads

risenW commented 3 years ago

FYI, this can be fixed this way: https://www.typescriptlang.org/docs/handbook/2/functions.html#function-overloads

Thanks for sharing. Would you like to work on this fix?

mebibou commented 3 years ago

hum maybe later, I currently cant even use the library because I am on a M1 Mac so...

risenW commented 2 years ago

FIXED and released in the latest TS version.

If you need help migrating to the latest version, see: https://danfo.jsdata.org/examples/migrating-to-the-stable-version-of-danfo.js