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

Fix breaking type definition about typescript #215

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions __tests__/typescript/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { DocumentPicker} from "react-native-document-picker";

// Option is correct about pick

DocumentPicker.pick({
type: [DocumentPicker.types.allFiles]
})

DocumentPicker.pick({
type: [DocumentPicker.types.audio]
})

DocumentPicker.pick({
type: [DocumentPicker.types.images]
})

DocumentPicker.pick({
type: [DocumentPicker.types.plainText]
})

DocumentPicker.pick({
type: [DocumentPicker.types.video]
})

DocumentPicker.pick({
type: [DocumentPicker.types.pdf]
})

DocumentPicker.pick({
type: [DocumentPicker.types.video,DocumentPicker.types.pdf, 'public.audio']
})

// Option is correct about pickMultiple

DocumentPicker.pickMultiple({
type: [DocumentPicker.types.allFiles]
})

DocumentPicker.pickMultiple({
type: [DocumentPicker.types.allFiles]
})

DocumentPicker.pickMultiple({
type: [DocumentPicker.types.audio]
})

DocumentPicker.pickMultiple({
type: [DocumentPicker.types.images]
})

DocumentPicker.pickMultiple({
type: [DocumentPicker.types.plainText]
})

DocumentPicker.pickMultiple({
type: [DocumentPicker.types.video]
})

DocumentPicker.pickMultiple({
type: [DocumentPicker.types.pdf]
})

DocumentPicker.pickMultiple({
type: [DocumentPicker.types.video,DocumentPicker.types.pdf, 'public.audio']
})

try {
throw new Error('test')
} catch (e) {
if(DocumentPicker.isCancel(e)){

} else {

}
}
51 changes: 42 additions & 9 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,54 @@
declare module 'react-native-document-picker' {
interface DocumentPickerOptions {
filetype: Array<string>;
multiple?: Boolean;
type Types = {
mimeTypes: {
allFiles: '*/*',
audio: 'audio/*',
images: 'image/*',
plainText: 'text/plain',
pdf: 'application/pdf',
video: 'video/*',
},
utis: {
allFiles: 'public.content',
audio: 'public.audio',
images: 'public.image',
plainText: 'public.plain-text',
pdf: 'com.adobe.pdf',
video: 'public.movie',
},
extensions: {
allFiles: '*',
audio:
'.3g2 .3gp .aac .adt .adts .aif .aifc .aiff .asf .au .m3u .m4a .m4b .mid .midi .mp2 .mp3 .mp4 .rmi .snd .wav .wax .wma',
images: '.jpeg .jpg .png',
plainText: '.txt',
pdf: '.pdf',
video: '.mp4',
},
};
type PlatformTypes = {
android: Types['mimeTypes']
ios: Types['utis']
windows: Types['extensions']
};
interface DocumentPickerOptions<OS extends keyof PlatformTypes> {
type: Array<PlatformTypes[OS][keyof PlatformTypes[OS]]>
}
interface DocumentPickerResponse {
uri: string;
type: string;
fileName: string;
fileSize: string;
}
export class DocumentPicker {
static pick(
options: { multiple: false } & DocumentPickerOptions
type Platform = 'ios' | 'android' | 'windows'
export class DocumentPicker<OS extends keyof PlatformTypes = Platform> {
static types: PlatformTypes['ios'] | PlatformTypes['android'] | PlatformTypes['windows']
static pick<OS extends keyof PlatformTypes = Platform>(
options: DocumentPickerOptions<OS>
): Promise<DocumentPickerResponse>;
static pickMultiple(
options: { multiple: true } & DocumentPickerOptions
static pickMultiple<OS extends keyof PlatformTypes = Platform>(
options: DocumentPickerOptions<OS>
): Promise<DocumentPickerResponse>;
static isCancel(err: any): void;
static isCancel<IError extends {code?: string}>(err?: IError): boolean;
}
}
7 changes: 7 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"baseUrl": "./__tests__/typescript" ,
"types": ["./","./node_modules"]
},
"include": ["./"]
}