You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@cordova.apache.org by GitBox <gi...@apache.org> on 2020/06/24 17:27:52 UTC

[GitHub] [cordova-plugin-file] kputh opened a new issue #402: Adjust type definitions to work without type imports

kputh opened a new issue #402:
URL: https://github.com/apache/cordova-plugin-file/issues/402


   # Bug Report
   
   ## Problem
   
   ### What is expected to happen?
   
   The following code should compile without adding any imports:
   
   ```
   export async function resolveLocalFileSystemURL(url: string): Promise<Entry> {
       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<DirectoryEntry> {
       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
   <!-- Include all relevant information that might help understand and reproduce the problem -->
   
   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. `/// <reference types="cordova-plugin-file" />` 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<AsyncDatabase> {
       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
   <!-- 
   What are relevant versions you are using?
   For example:
   Cordova: Cordova CLI, Cordova Platforms, Cordova Plugins 
   Other Frameworks: Ionic Framework and CLI version
   Operating System, Android Studio, Xcode etc.
   -->
   * cordova-plugin-file 6.0.2
   * typescript 3.8.3
   
   
   
   ## Checklist
   <!-- Please check the boxes by putting an x in the [ ] like so: [x] -->
   
   - [x] I searched for existing GitHub issues
   - [ ] I updated all Cordova tooling to most recent version
   - [x] I included all the necessary information above
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org


[GitHub] [cordova-plugin-file] breautek commented on issue #402: Adjust type definitions to work without type imports

Posted by GitBox <gi...@apache.org>.
breautek commented on issue #402:
URL: https://github.com/apache/cordova-plugin-file/issues/402#issuecomment-669829161


   > When I use cordova-sqlite-storage with the types from DefinitelyTyped, it works out of the box and without any imports:
   
   What's actually happening here is that when you install `@types/cordova-sqlite-storage` or any other typings from the `@types` organization, they get placed in `node_modules/@types/...` which typescript will automatically load by default.
   
   Typings that is provided by `cordova-plugin-file` I believe is only ever imported when you explicitly have an `import` statement (which doesn't make sense in this case as cordova plugins don't actually export anything, as they clobber the global namespace).
   
   Using `/// <reference types="cordova-plugin-file" />` like you said should work. You can also try messing with the [typeRoots](https://www.typescriptlang.org/tsconfig#typeRoots) TS compiler flag.
   
   There isn't anything Cordova can "adjust" to make this work out of the box. But I do believe this should be documented somewhere so I'll leave this issue open.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org