-
Notifications
You must be signed in to change notification settings - Fork 106
DirectoryTree - Missing custom field support in typescript interface #103
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
Comments
@lukasrandom Seems like this was merged with the fix you indicated, but even though it solves the issue you faced, I feel like that doesn't really provide meaningful typing to the custom object. That is, when I add properties to the Would it not be better - developer experience wise - to make the // node_modules/directory_tree/index.d.ts
import { Stats } from 'fs';
interface IObj {
[key: string]: any;
}
declare function directoryTree<TCustomFile extends IObj = IObj, TCustomDir extends IObj = IObj, TCustomResult = TCustomFile & TCustomDir>(
path: string,
options?: directoryTree.DirectoryTreeOptions,
onEachFile?: directoryTree.DirectoryTreeCallback<TCustomFile>,
onEachDirectory?: directoryTree.DirectoryTreeCallback<TCustomDir>,
): directoryTree.DirectoryTree<TCustomResult>;
export as namespace directoryTree;
declare namespace directoryTree {
export interface DirectoryTree<C extends IObj = IObj> {
path: string;
name: string;
size: number;
type: "directory" | "file";
children?: DirectoryTree<C>[];
extension?: string;
isSymbolicLink?: boolean;
custom: C;
}
export interface DirectoryTreeOptions {
normalizePath?: boolean;
exclude?: RegExp | RegExp[];
attributes?: (keyof Stats | "type" | "extension")[];
extensions?: RegExp;
followSymlinks?: boolean;
depth?: number;
}
export type DirectoryTreeCallback<TCustom extends IObj = IObj> = (item: DirectoryTree<TCustom>, path: string, stats: Stats) => void;
}
export = directoryTree; This should not change the current functionality, but will allow developers to have more fine grained control over the For example, if I don't want the export type TDirectoryTree = DirectoryTree<TCustomFile & TCustomDir>; and use it throughout my app instead of the default @mihneadb if you agree with the above I can make a PR 🙂 |
@lbragile sorry about the terribly late reply - I think this would complicate use more for the "average user". Thoughts of other options perhaps? |
@mihneadb No worries. I will have to disagree with you on:
as my proposed typing should not cause anyone to update anything due to the same default types. That is, what I suggested only provides users the ability to "opt into" custom typing if they choose to - hence the generic interfaces. On that note, I decided to drop this package in favor of a custom solution for my needs, so feel free to use the above if you'd like. If you do end up using it, I would appreciate a note crediting me 🙂 |
@lbragile my bad, I misread your proposal as something that would implicitly require folks to provide explicit typing always. Thanks for having the patience to reply to that. :) A PR sounds great! |
Hey,
just tried to add custom field to DirectoryTree object in typescript inside a
DirectoryTreeCallback
. The following error occured:Error:
Example adding custom
id
field:Fix:
Added
[key: string]: any
in to theDirectoryTree
interface atnode_modules\directory-tree\index.d.ts
.I will send you a PR so you can decide if this is a reasonable fix for it.
Best
Lukas
The text was updated successfully, but these errors were encountered: