-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enrich the docs when it comes to types #269
Conversation
Great work on this repo @iambumblehead, it made our lives way easier 🙏 |
👍 |
test('allows you to specify the type of returned exports', async () => {
const { multiplyNumbers } = await esmock<typeof import('../src/utils.js')>(
'../src/utils.js', {
multiplyNumbers: (numbers: number[]): number => numbers.reduce((acc, current) => acc *= current, 1),
})
assert.equal(multiplyNumbers([1, 2, 3]), 6)
}) The example only works if strange things happen,
test('allows you to specify the type of returned exports', async () => {
const { multiplyNumbers } = await esmock('../src/utils.js', {
multiplyNumbers: numbers => numbers.reduce((acc, current) => acc *= current, 1)
})
assert.equal(multiplyNumbers([1, 2, 3]), 6)
}) The example would be easier to follow if utils used multiply in a more meaningful way and if it imported multiply from an obvious moduleId or path such as "../src/numbers.js", eg something like this, test('allows you to specify the type of returned exports', async () => {
const { makebabies } = await esmock('../src/rabbits.js', {
'../src/numbers.js': {
multiply: numbers => numbers.reduce((acc, current) => acc *= current, 1)
}
})
assert.equal(makebabies({ pairs: 3 }).length, 28)
}) |
@ahmed-hritani would you remake the README example to follow these points?
|
I like this example and I like if this is added to a separated block from the javascript one,
import test from 'node:test'
import assert from 'node:assert'
import esmock from 'esmock'
import type Rabbit from '../rabbit.js';
test('specify the type of export returned', async () => {
const rabbit = await esmock<Rabbit>('../rabbit.js', {
'../util.js': {
multiply: (numbers: number[]): number => (
numbers.reduce((acc, n) => acc *= n, 1))
}
})
assert.equal(rabbit.makebabies({ pairs: 3 }), '🐇'
+ '🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇'
+ '🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇'
+ '🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇🐇')
}) |
Thank you for your comments @iambumblehead, I created a new PR with an example that (hopefully) follow the constraints and added the constraints to the |
I noticed that after the addition of #267, there is nothing in the docs to show that
esmock
is now type generic.