Skip to content

Commit

Permalink
fix: update return types
Browse files Browse the repository at this point in the history
  • Loading branch information
kgryte committed Oct 26, 2024
1 parent d9db39a commit c615a6f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
10 changes: 5 additions & 5 deletions lib/node_modules/@stdlib/fs/read-json/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ type Reviver = ( key: string, value: any ) => any;
* Callback invoked upon reading a file.
*
* @param err - error object
* @param file - file contents
* @param data - file contents
*/
type Callback = ( err: Error | null, file: Buffer | string ) => void;
type Callback<T> = ( err: Error | null, data: T ) => void;

/**
* Interface for reading a file as JSON.
Expand Down Expand Up @@ -85,7 +85,7 @@ interface ReadJSON {
* console.dir( data );
* }
*/
( file: string | Buffer | number, options: Options | string, clbk: Callback ): void;
<T = unknown>( file: string | Buffer | number, options: Options | string, clbk: Callback<T> ): void;

/**
* Asynchronously reads a file as JSON.
Expand All @@ -105,7 +105,7 @@ interface ReadJSON {
* console.dir( data );
* }
*/
( file: string | Buffer | number, clbk: Callback ): void;
<T = unknown>( file: string | Buffer | number, clbk: Callback<T> ): void;

/**
* Synchronously reads a file as JSON.
Expand All @@ -127,7 +127,7 @@ interface ReadJSON {
* }
* console.dir( out );
*/
sync( file: string | Buffer | number, options?: Options | string ): string | Error;
sync<T = unknown>( file: string | Buffer | number, options?: Options | string ): T | Error;
}

/**
Expand Down
16 changes: 11 additions & 5 deletions lib/node_modules/@stdlib/fs/read-json/docs/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@

import readJSON = require( './index' );

const onLoad = ( error: Error | null, file: string | Buffer ) => {
if ( error || !file ) {
/**
* Callback function.
*
* @param error - error object
* @param data - results
*/
function onLoad( error: Error | null, data?: Array<string> | Buffer ): void {
if ( error || !data ) {
throw error;
}
};
}


// TESTS //
Expand Down Expand Up @@ -105,9 +111,9 @@ const onLoad = ( error: Error | null, file: string | Buffer ) => {
readJSON( 'C:\\foo\\bar\\baz\\package.json' ); // $ExpectError
}

// Attached to main export is a `sync` method which returns a string or an error...
// Attached to main export is a `sync` method which returns results or an error...
{
readJSON.sync( 'package.json' ); // $ExpectType string | Error
readJSON.sync<Array<string>>( 'package.json' ); // $ExpectType Error | string[]
}

// The compiler throws an error if the `sync` method is provided a first argument which is not a string, buffer, or file descriptor...
Expand Down

1 comment on commit c615a6f

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report

Package Statements Branches Functions Lines
fs/read-json $\color{green}268/268$
$\color{green}+100.00\%$
$\color{green}38/38$
$\color{green}+100.00\%$
$\color{green}3/3$
$\color{green}+100.00\%$
$\color{green}268/268$
$\color{green}+100.00\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.