From a0d4ebcb7472ecb25e9e0d87dd717f4be7e08475 Mon Sep 17 00:00:00 2001 From: Ali Taheri Moghaddar Date: Sun, 14 May 2017 13:42:16 +0430 Subject: [PATCH 1/2] Improve typings for react-file-reader-input --- types/react-file-reader-input/index.d.ts | 54 ++++++++++++++----- .../react-file-reader-input-tests.tsx | 8 +-- types/react-file-reader-input/tslint.json | 1 + 3 files changed, 46 insertions(+), 17 deletions(-) create mode 100644 types/react-file-reader-input/tslint.json diff --git a/types/react-file-reader-input/index.d.ts b/types/react-file-reader-input/index.d.ts index 37f9357f5c..fc172a425d 100644 --- a/types/react-file-reader-input/index.d.ts +++ b/types/react-file-reader-input/index.d.ts @@ -1,19 +1,47 @@ -// Type definitions for react-file-reader-input +// Type definitions for react-file-reader-input 1.1 // Project: https://www.npmjs.com/package/react-file-reader-input -// Definitions by: Dmitry Rogozhny +// Definitions by: Dmitry Rogozhny , Ali Taheri // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.1 -/// +import * as React from 'react'; -declare module "react-file-reader-input" { - interface FileInputProps { - as?: string; - onChange?: (event: React.SyntheticEvent, results: any) => void; - } - - class FileInput extends React.Component { - } - - export = FileInput; +declare class FileInput extends React.Component { } + +declare namespace FileInput { + type Format = 'buffer' | 'binary' | 'url' | 'text'; + type Result = [ProgressEvent, File]; + + interface Props { + /** + * what format the `FileReader` should read the file as + * (i.e., `'buffer'`, `'binary'`, `'url'`, `'text'`). + * + * Defaults to `'url'`. + */ + as?: Format; + + /** + * Callback function called when the files are choosen by the user. + * + * Results will be an array of arrays, the size of which depending + * on how many files were selected. + * + * Each result will be an array of two items: + * + * `progressEvent`: `result[0]` is a `ProgressEvent` object. + * You can retrieve the raw results at `progressEvent.target.result` + * among other things. + * + * `file`: `result[1]` is a `File` object. You can retrieve the file name + * at file.name among other things. + * + * @param event The event that triggered file changes + * @param results The array of files + */ + onChange: (event: React.SyntheticEvent, results: Result[]) => void; + } +} + +export = FileInput; diff --git a/types/react-file-reader-input/react-file-reader-input-tests.tsx b/types/react-file-reader-input/react-file-reader-input-tests.tsx index 0b21edfd71..1ee4f2f699 100644 --- a/types/react-file-reader-input/react-file-reader-input-tests.tsx +++ b/types/react-file-reader-input/react-file-reader-input-tests.tsx @@ -1,9 +1,9 @@ -import * as React from "react"; -import FileReaderInput = require("react-file-reader-input"); +import * as React from 'react'; +import * as FileReaderInput from 'react-file-reader-input'; class MyComponent extends React.Component<{}, {}> { - handleChange = (event: React.SyntheticEvent, results: any) => { - results.forEach((result: any) => { + handleChange = (event: React.SyntheticEvent, results: FileReaderInput.Result[]) => { + results.forEach(result => { const [event, file] = result; console.log(`Selected file ${file.name}!`); }); diff --git a/types/react-file-reader-input/tslint.json b/types/react-file-reader-input/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/react-file-reader-input/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From 38c47c7e190d4bbd06992ee2d56f5348452b4e7a Mon Sep 17 00:00:00 2001 From: Ali Taheri Moghaddar Date: Sun, 14 May 2017 14:37:39 +0430 Subject: [PATCH 2/2] Fix lint errors --- types/react-file-reader-input/index.d.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/types/react-file-reader-input/index.d.ts b/types/react-file-reader-input/index.d.ts index fc172a425d..3caed3dc67 100644 --- a/types/react-file-reader-input/index.d.ts +++ b/types/react-file-reader-input/index.d.ts @@ -17,30 +17,30 @@ declare namespace FileInput { /** * what format the `FileReader` should read the file as * (i.e., `'buffer'`, `'binary'`, `'url'`, `'text'`). - * + * * Defaults to `'url'`. */ as?: Format; /** * Callback function called when the files are choosen by the user. - * + * * Results will be an array of arrays, the size of which depending * on how many files were selected. - * + * * Each result will be an array of two items: - * + * * `progressEvent`: `result[0]` is a `ProgressEvent` object. * You can retrieve the raw results at `progressEvent.target.result` * among other things. - * + * * `file`: `result[1]` is a `File` object. You can retrieve the file name * at file.name among other things. - * + * * @param event The event that triggered file changes * @param results The array of files */ - onChange: (event: React.SyntheticEvent, results: Result[]) => void; + onChange(event: React.SyntheticEvent, results: Result[]): void; } }