Typescript example
See original GitHub issueSeems that ts-loader
and/or awesome-typescript-loader
don’t play nicely with the async->sync import solution bindgen produces.
This was already raised and closed here: https://github.com/rustwasm/wasm-bindgen/issues/700 but it might be helpful to have a Typescript boilerplate as a reference someday 😃
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:6
Top Results From Across the Web
Documentation - TypeScript for JavaScript Programmers
TypeScript knows the JavaScript language and will generate types for you in many cases. For example in creating a variable and assigning it...
Read more >Typescript example: Beginners Guide To ... - AppDividend
Typescript is a typed superset of Javascript that compiles to plain JavaScript. TypeScript is open source language maintained by Microsoft.
Read more >TypeScript Tutorial - W3Schools
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python,...
Read more >TypeScript Tutorial - Tutorialspoint
TypeScript is pure object oriented with classes, interfaces and statically typed like C# or Java. The popular JavaScript framework Angular 2.0 is written...
Read more >TypeScript Tutorial: What is, Interface, Enum, Array with Example
Above TypeScript example throws an error, but the same would have worked fine if it was with the var keyword. Variables using let...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
TL;DR: The magic tricks are:
"module": "esNext"
intsconfig.json
await import('./mymod')
typeof import('./mymod')
Details
In your
tsconfig.json
, set"module": "esNext"
so that you can useasync import(...)
:In your
webpack.config.js
, enablets-loader
(as per the usual instructions) and the experimental built-in*.wasm
loader:Finally, let’s assume you have the following files generated by
wasm-bindgen
:mymod.js
mymod.d.ts
mymod_bg.wasm
Then you can use a structure like this in your top-level
*.ts
file:The magic trick here is
typeof import('./mymod')
, which allows you declare the type of the asynchronously-loaded module.I’d be happy to try to turn this into a fully-worked test case that can run on CI and that can be used as example, but it might need to wait a few days until I have time.
wasm-pack-plugin
version0.1.0
was recently released and among other improvements, it now has the option to configurewasm-pack
to generate TypeScript bindings that can be used in your code.Here’s an example webpack configuration to make use of it:
webpack.config.js
tsconfig.json
example
index.ts