apache / apache/cordova-plugin-file

Adjust type definitions to work without type imports

Open
#402 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
744
Forks
754
Avg merge
3d 4h
Merged PRs (30d)
1

Description

# Bug Report

## Problem

### What is expected to happen?

The following code should compile without adding any imports:

```
export async function resolveLocalFileSystemURL(url: string): Promise {
if ('function' !== typeof window?.resolveLocalFileSystemURL) {
throw new Error('window.resolveLocalFileSystemURL is not a function');
}
return new Promise((resolve, reject) => window.resolveLocalFileSystemURL(url, resolve, reject));
}

export async function getDirectory(directoryEntry: DirectoryEntry,
path: string, options?: Flags): Promise {
return new Promise((resolve, reject) => directoryEntry.getDirectory(path, options, resolve, reject));
}
```

### What does actually happen?

```
error TS2304: Cannot find name 'Entry'.
error TS2339: Property 'resolveLocalFileSystemURL' does not exist on type 'Window & typeof globalThis'.
error TS2339: Property 'resolveLocalFileSystemURL' does not exist on type 'Window & typeof globalThis'.
error TS2304: Cannot find name 'DirectoryEntry'.
error TS2304: Cannot find name 'Flags'.
error TS2304: Cannot find name 'DirectoryEntry'.
```

## Information

It seems that I cannot use this plugin without explicitly importing the type declarations. But (most) import syntaxes are meant to import types **and implementations** – the latter not being provided in any way by npm standards.

(When I add `import 'cordova-plugin-file';` it tells me that the definition file is no module. `/// ` works, but arriving at this workaround took quite a while.)

When I use _cordova-sqlite-storage_ with the types from _DefinitelyTyped_, it works out of the box and without any imports:

```
export async function openDatabase(args: SQLitePlugin.OpenArgs): Promise {
if ('function' !== typeof window?.sqlitePlugin?.openDatabase) {
throw new Error('window.sqlitePlugin.openDatabase is not a function');
}
return new Promise((resolve, reject) =>
window.sqlitePlugin.openDatabase(args, db => resolve(new AsyncDatabase(db)), reject));
}
```
The difference is likely the namespace declaration in _cordova-sqlite-storage's_ type definition file.

### Version information

* cordova-plugin-file 6.0.2
* typescript 3.8.3

## Checklist

- [x] I searched for existing GitHub issues
- [ ] I updated all Cordova tooling to most recent version
- [x] I included all the necessary information above

Contributor guide

Open the contributing guide

Research direction

Inspect the plugin's type declaration and compare it with the cordova-sqlite-storage DefinitelyTyped declaration, especially its namespace pattern. Reproduce the TypeScript 3.8.3 errors using the example; done when the shown code compiles without explicit type imports and the declarations expose the Cordova file types.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
developer-experience, mobile-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.