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

Would you consider adding Typescript type definitions? #15

Closed
artis3n opened this issue Oct 8, 2020 · 6 comments
Closed

Would you consider adding Typescript type definitions? #15

artis3n opened this issue Oct 8, 2020 · 6 comments

Comments

@artis3n
Copy link

artis3n commented Oct 8, 2020

I saw #7 adding support for ES6 modules, which is convenient. I currently use this library in a Typescript project via

import { list as rraList } from 'recursive-readdir-async';

Typescript throws a compilation error (that I have to ignore) saying:

TS7016: Could not find a declaration file for module 'recursive-readdir-async'. '/<redacted>/node_modules/recursive-readdir-async/build/module.cjs.js' implicitly has an 'any' type.   Try `npm install @types/recursive-readdir-async` if it exists or add a new declaration (.d.ts) file containing `declare module 'recursive-readdir-async';`

I am not super familiar on how to do that myself, but I see many projects have type definitions under @types. The documentation for that appears to be https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html#publish-to-types. This page also has some documentation on how to build in typescript types natively to your library.

Is this something you'd consider doing? Would be a convenience when using this library with typescript. I currently suppress the error with:

// @ts-ignore
import { list as rraList } from 'recursive-readdir-async';
@m0rtadelo
Copy link
Owner

I will add @types to avoid TypeScript errors as soon as possible. Thank you for your feedback!

@artis3n
Copy link
Author

artis3n commented Oct 14, 2020

FWIW here are the types I created for my use of the list function's output, where list returns a DiscoveredFilesResult and is invoked as:

return await rraList(dirPath, options);

types:

export type DiscoveredFilesResult = Array<FileDiscoveredItem> | Array<RecursiveReaddirSysError> | Array<RecursiveReaddirFileError>;

export interface FileDiscoveredItem {
  name: string;
  path: string;
  fullname: string;
  isDirectory: boolean;
}

export interface RecursiveReaddirSysError {
  error: {
    message: string;
    errno: number;
    code: string;
    syscall: string;
    path: string;
  };
  path: string;
}

export interface RecursiveReaddirFileError {
  name: string;
  title: string;
  path: string;
  fullname: string;
  error: object;
}

Had to define this function as well to get Typescript to realize when the result is not an error:

function isFileDiscoveredItemArray(
  item: DiscoveredFilesResult
): item is Array<FileDiscoveredItem> {
  return !("error" in item[0]);
}

which comes from https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards.

@artis3n
Copy link
Author

artis3n commented Jan 15, 2021

@m0rtadelo Are you still planning on doing this? I hit a undefined result issue that I am pretty sure would be prevented if the types I defined for this library were accurate :D

@m0rtadelo
Copy link
Owner

m0rtadelo commented Jan 21, 2021 via email

@artis3n
Copy link
Author

artis3n commented Jan 21, 2021

That is fair!

@m0rtadelo
Copy link
Owner

Version 1.2.0 has been written totally in typescript and works with types in typescript projects as expected. Sorry for the wait

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants