Skip to content

Fix global Cypress namespace augumentation#14

Merged
jm-mailosaur merged 2 commits intomailosaur:masterfrom
miluoshi:master
Mar 17, 2021
Merged

Fix global Cypress namespace augumentation#14
jm-mailosaur merged 2 commits intomailosaur:masterfrom
miluoshi:master

Conversation

@miluoshi
Copy link
Contributor

@miluoshi miluoshi commented Mar 10, 2021

This is fixed with 2 things:

  1. For global namespace augumentation to work correctly, the augumentation needs to be in an "ambient" context (vs module context).
    You can achieve ambient context in d.ts files in 2 ways:
    a.) by not using import/export keywords (using them toggles module context)
    b.) by wrapping namespace augumentation in declare global {} block - ambient context is inside this block.

See more info here:

  1. For Typescript to be able to use provided type declarations, we need to point it to a correct d.ts file.
    This is done by specifying path to this file in package.json's field "types".
    With this, an application just needs to import 'cypress-mailosaur' and types just work

fixes #12

This is fixed with 2 things:

1. For global namespace augumentation to work correctly, the augumentation needs to be in an "ambient" context (vs module context).
You can achieve ambient context in d.ts files in 2 ways:
  a.) by not using import/export keywords (using them toggles module context)
  b.) by wrapping namespace augumentation in `declare global {}` block - ambient context is inside this block.

See more info here:
- https://www.typescriptlang.org/docs/handbook/release-notes/typescript-1-8.html#augmenting-globalmodule-scope-from-modules
- microsoft/TypeScript-Handbook#180 (comment)

2. For Typescript to be able to use provided type declarations, we need to point it to a correct d.ts file.
This is done by specifying path to this file in package.json's field "types".
With this, an application just needs to `import 'cypress-mailosaur'` and types just work
@miluoshi miluoshi mentioned this pull request Mar 10, 2021
@adjerbetian
Copy link

Just a suggestion en passant (I know it was like this before the PR):

I've usually seen a package's definitions in a index.d.ts at the package's root folder, next to the index.js. It won't change anything for a consumer point of view, but it feels weird to have the definitions inside src/ where it's actually not used.

@jm-mailosaur jm-mailosaur merged commit 66d709a into mailosaur:master Mar 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Usage with TypeScript

3 participants