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

Add TypeScript definition #15

Merged
merged 22 commits into from
Sep 18, 2018
Merged

Conversation

DenisCarriere
Copy link
Contributor

@DenisCarriere
Copy link
Contributor Author

Issue again with xo handling Typescript definitions

$ npm t

> load-json-file@5.0.0 test /Users/denis/Github/load-json-file
> xo && ava


  index.d.ts:1:1
  ✖  1:1  Parsing error: The keyword interface is reserved

  1 error
npm ERR! Test failed.  See above for more details.

@DenisCarriere
Copy link
Contributor Author

Updated PR based on comments from => sindresorhus/write-json-file#17

CC: @sindresorhus

index.d.ts Outdated
* console.log('done');
* })();
*/
export default function loadJsonFile(filepath: string, options?: Options): Promise<void>;
Copy link
Contributor

Choose a reason for hiding this comment

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

It returns a Promise<any>. What we can do however is make it generic like this

export default function loadJsonFile<T = any>(filepath: string, options?: Options): Promise<T>;

Then the user can supply the type that it returns.

interface Foo {
    value: string;
}

const data = await loadJsonFile<Foo>('data.json');
//=> data is typed as `Foo`

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 you're correct, it doesn't return Promise<void> 🤦‍♂️

I like using the generic approach 👍

index.d.ts Outdated
* loadJsonFile.sync('foo.json')
* console.log(json);
*/
export function sync(filepath: string, options?: Options): void;
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as below, it returns an any object but we can make it a generic.

index.d.ts Outdated
* @example
* import * as loadJsonFile from 'load-json-file';
*
* loadJsonFile.sync('foo.json')
Copy link
Contributor

Choose a reason for hiding this comment

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

const json = loadJsonFile.sync('foo.json');

index.d.ts Outdated
*
* (async () => {
* await loadJsonFile('foo.json');
* console.log('done');
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we use the same example as above and do console.log(json) instead? Because the functionality is the same, it's just async.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed! I've updated both examples to reflect the same logic

@DenisCarriere
Copy link
Contributor Author

@SamVerschueren Pushed updates based on your comments.

Thanks for reviewing!

Copy link
Contributor

@SamVerschueren SamVerschueren left a comment

Choose a reason for hiding this comment

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

Added two extra comments. Thanks for working on this!

index.d.ts Outdated
/**
* Applies a function to the JSON string before parsing.
*/
beforeParse?: Function
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can be more specific on what the Function is and should return.

beforeParse?: (data: string) => string;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Totally agree 😄

index.d.ts Outdated
* Prescribes how the value originally produced by parsing is transformed, before being returned.
* See the [JSON.parse docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Using_the_reviver_parameter) for more.
*/
reviver?: Function
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above, we can be more specific.

reviver?: (key: string, value: any) => any;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 agreed

@DenisCarriere
Copy link
Contributor Author

@SamVerschueren Updated Function

Copy link
Contributor

@SamVerschueren SamVerschueren left a comment

Choose a reason for hiding this comment

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

Looks good to me!

@sindresorhus sindresorhus changed the title Add Typescript Definition Add TypeScript definition Aug 27, 2018
index.d.ts Outdated
* @example
* import * as loadJsonFile from 'load-json-file';
*
* const json = loadJsonFile.sync('foo.json')
Copy link
Owner

Choose a reason for hiding this comment

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

Semicolon

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

index.d.ts Outdated
* import * as loadJsonFile from 'load-json-file';
*
* const json = loadJsonFile.sync('foo.json')
* console.log(json);
Copy link
Owner

Choose a reason for hiding this comment

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

Let's drop the console.log

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

index.d.ts Outdated
* //=> {foo: true}
* })();
*/
export default function loadJsonFile<T = any>(filepath: string, options?: Options): Promise<T>;
Copy link
Owner

Choose a reason for hiding this comment

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

Should we use unknown here instead of any?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤔 unknown isn't a type in Typescript, any would be considered the "unknown" since any can be applied to anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh I didn't know this was a new type in Typescript 3.x

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-0.html

Learn something every day!

index.d.ts Outdated
* console.log(json);
* //=> {foo: true}
*/
export function sync<T = any>(filepath: string, options?: Options): T;
Copy link
Owner

Choose a reason for hiding this comment

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

filepath => filePath (I plan to make the same change in the code and docs after merging this)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 Done

Copy link
Contributor

@SamVerschueren SamVerschueren left a comment

Choose a reason for hiding this comment

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

Can you add tsd-check? For an example, see write-json-file.

@DenisCarriere
Copy link
Contributor Author

@SamVerschueren Sure I'll try to get something started, it won't be as elaborate as yours, but at least it will be a start

package.json Outdated
@@ -36,6 +36,7 @@
},
"devDependencies": {
"ava": "*",
"tsd-check": "^0.1.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

I just released a new version (0.2.1) which supports top-level await.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 awesome! Changed it to "*"

index.d.ts Outdated
* //=> {foo: true}
* })();
*/
export default function loadJsonFile<T = any>(filePath: string, options?: Options): Promise<T>;
Copy link
Owner

Choose a reason for hiding this comment

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

I think we should use unknown instead of any so users are forced to explicitly coerce it. Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 unknown works as the default

Very cool advanced type from Typescript 3.x

@DenisCarriere
Copy link
Contributor Author

@sindresorhus @SamVerschueren anything else to review on this PR?

@sindresorhus
Copy link
Owner

Looks good. Thanks for your work and patience :)

@sindresorhus sindresorhus merged commit a58cdf3 into sindresorhus:master Sep 18, 2018
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

Successfully merging this pull request may close these issues.

None yet

3 participants