Skip to content

Commit

Permalink
🤖 Merge PR DefinitelyTyped#64020 fix(react-native-background-download…
Browse files Browse the repository at this point in the history
…er): correct types for DownloadTaskState by @fivecar

It turns out that react-native-background-downloader doesn't actually export this type. Things may seem fine with doing things like `DownloadTaskState.DOWNLOADING` in your code, until you use that in a package that you also then publish yourself. At that point, you'll find the transpiled JS in your package refers to things like react_native_background_downloader_1.DownloadTaskState.DOWNLOADING, which definitely does not exist and will throw at runtime.
  • Loading branch information
fivecar authored and miccehedin committed Feb 15, 2023
1 parent 71cf231 commit b64b54b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
8 changes: 1 addition & 7 deletions types/react-native-background-downloader/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,7 @@ export type BeginHandler = ({ expectedBytes, headers }: BeginHandlerObject) => a
export type ProgressHandler = (percent: number, bytesWritten: number, totalBytes: number) => any;
export type DoneHandler = () => any;
export type ErrorHandler = (error: any, errorCode: any) => any;
export enum DownloadTaskState {
DOWNLOADING = 'DOWNLOADING',
PAUSED = 'PAUSED',
DONE = 'DONE',
FAILED = 'FAILED',
STOPPED = 'STOPPED',
}
export type DownloadTaskState = 'DOWNLOADING' | 'PAUSED' | 'DONE' | 'FAILED' | 'STOPPED';

export interface DownloadTask {
constructor: (taskInfo: TaskInfo) => DownloadTask;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import RNBackgroundDownloader, {
completeHandler,
DownloadTask,
DownloadTaskState,
} from 'react-native-background-downloader';
import RNBackgroundDownloader, { completeHandler, DownloadTask } from 'react-native-background-downloader';

// Set global headers for downloader
RNBackgroundDownloader.setHeaders({
Expand Down Expand Up @@ -35,24 +31,25 @@ const task = RNBackgroundDownloader.download({
const taskFuncTest = (task: DownloadTask) => {
// Check task state
switch (task.state) {
case DownloadTaskState.DONE: {
case 'DONE': {
console.log('Task is in state DONE');
break;
}
case DownloadTaskState.DOWNLOADING: {
case 'DOWNLOADING': {
console.log('Task is in state DOWNLOADING');
break;
}
case DownloadTaskState.PAUSED: {
case 'PAUSED': {
console.log('Task is in state PAUSED');
break;
}
case DownloadTaskState.FAILED: {
case 'FAILED': {
console.log('Task is in state FAILED');
break;
}
case DownloadTaskState.STOPPED: {
case 'STOPPED': {
console.log('Task is in state STOPPED');
break;
}
}

Expand Down

0 comments on commit b64b54b

Please sign in to comment.