Skip to content

Instantly share code, notes, and snippets.

@slimlime
Last active July 1, 2018 06:01
Show Gist options
  • Save slimlime/fc4cf1a841cd5be0b28612707470e468 to your computer and use it in GitHub Desktop.
Save slimlime/fc4cf1a841cd5be0b28612707470e468 to your computer and use it in GitHub Desktop.
/*
* @Author : SLim
* @Date : 2018-06-30 13: 58: 47
* @Last Modified time: 2018-06-30 15: 04: 36
*/
// Limited-use, specific problem.
// Showcases a workaround to cast namespace/module definition onto an "any" typed module (because using JavaScript node module require)
// For autocompletion usage, typing to speed up development.
// I ended up figuring this workaround after a long time spend troubleshooting hard-to-track down different dependencies issues.
// Hopefully can determine later whether it was a webpack issue or something simple.
// Please comment if you have come across this problem before and refer to this gist if it helps you.
// For the specific subset of the community who are newbie developers and suddenly want to learn Angular, Electron, Ionic, Node and
// web-building technologies all at once while taking advantage of TypeScript.
// Now you can avoid using some of the less-supported external wrappers that do not behave as intended and aren't typed.
// # Steps:
// Install package you want to use @example `fs-extra`
// - yarn add fs-extra @type/fs-extra
/// ```ts
import * as fsExtraNameSpace from 'fs-extra';
// Here is the magic line to get your IDE IntelliSense code-completion working again :tada: :lion: :gem:
const fsExtra: typeof fsExtraNameSpace = this.electronService.remote.require('fs-extra'); // also equivalent issue with node API require('fs')
// using the require() functions didn't preserve type information for VS Code, so leverage the @type information and re-enforce it!
const fsExtraDirContents = fsExtra.readdirSync('./'); // This gets the current directory contents. i.e. `ls` or `dir`
// And you're done!
/// ```
// Intrigued by the conflicts involved in importing filesystem APIs while using Electron (electron-builder, technologies etc)
// (ノಠ益ಠ)ノ彡┻━┻
// My first gist. Appreciate your comments if something is weird. Every time I encounter one of these modules
// I want to spend an inordinate amount of time learning how it works (the `small stuff`?) and have to re-prioritise the end-product.

/*

  • @Author : SLim
  • @Date : 2018-06-30 13: 58: 47
  • @Last Modified time: 2018-06-30 15: 04: 36 */

Limited-use, specific problem.

Description

Showcases a workaround to cast namespace/module definition onto an "any" typed module (because using JavaScript node module require) For autocompletion usage, typing to speed up development.

I ended up figuring this workaround after a long time spend troubleshooting hard-to-track down different dependencies issues. Hopefully can determine later whether it was a webpack issue or something simple. Please comment if you have come across this problem before and refer to this gist if it helps you. For the specific subset of the community who are newbie developers and suddenly want to learn Angular, Electron, Ionic, Node and web-building technologies all at once while taking advantage of TypeScript. Now you can avoid using some of the less-supported external wrappers that do not behave as intended and aren't typed.

Steps:

Install package you want to use @example fs-extra

yarn add fs-extra @type/fs-extra
import * as fsExtraNameSpace from 'fs-extra';

// Here is the magic line to get your IDE IntelliSense code-completion working again :tada: :lion: :gem:
const fsExtra: typeof fsExtraNameSpace = this.electronService.remote.require('fs-extra'); // also equivalent issue with node API require('fs')
// the require() functions didn't preserve type information for VS Code TS language service, so leverage the @type information and re-enforce it!
const fsExtraDirContents = fsExtra.readdirSync('./');                                     // This gets the current directory contents. i.e. `ls` or `dir`

//  And you're done!

🎉🎉🎉

Intellisense says: 'I is smart!'. but I wish the suggestion/snippet menu was more user-friendly and is mouse-interactable e.g. origin of some extension-based snippets? How do I easily pick the function overload that I want!

Intrigued by the conflicts involved in importing filesystem APIs while using Electron (electron-builder, technologies etc)

(ノಠ益ಠ)ノ彡┻━┻ - I hope this you can see this unicode stuff on the left of this text.

My first gist. Appreciate your comments if something is weird. Every time I encounter one of these modules I want to spend an inordinate amount of time learning how it works (the small stuff?) and have to re-prioritise the end-product.

@slimlime
Copy link
Author

slimlime commented Jun 30, 2018

Still interesting that the Node path API works without issues...
Too many fs conflicts.
~Also deprecated packages 🦁

@slimlime
Copy link
Author

slimlime commented Jul 1, 2018

Hmm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment