https://github.com/detachhead/typed-nodejs-assert
various fixes for nodeJS assertions in typescript.
https://github.com/detachhead/typed-nodejs-assert
assertions espower hacktoberfest nodejs power-assert typed-nodejs-assert typescript
Last synced: about 2 months ago
JSON representation
various fixes for nodeJS assertions in typescript.
- Host: GitHub
- URL: https://github.com/detachhead/typed-nodejs-assert
- Owner: DetachHead
- Created: 2021-01-07T10:35:43.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-11-24T15:03:54.000Z (over 4 years ago)
- Last Synced: 2025-02-21T00:08:19.089Z (over 1 year ago)
- Topics: assertions, espower, hacktoberfest, nodejs, power-assert, typed-nodejs-assert, typescript
- Language: TypeScript
- Homepage:
- Size: 351 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# typed nodejs assert
various fixes for nodeJS assertions in typescript.
[](https://www.npmjs.com/package/typed-nodejs-assert)
## what it do
this packages fixes the following issues:
- the typescript definitions for the default assertion library in nodeJS
[do not properly check the types of expected/actual values](https://github.com/DefinitelyTyped/DefinitelyTyped/pull/50274),
leading to assertion errors that could
easily be caught by the compiler.
for example. in the current type definitions, the following error would not occur:
```ts
type Foo = {
bar: number
}
const foo: Foo = {
bar: 1
}
//@ts-expect-error TS2345: Argument of type '{}' is not assignable to parameter of type 'Foo'. Property 'bar' is missing in type '{}' but required in type 'Foo'
assert.deepStrictEqual(foo, {})
```
- [power-assert](https://npmjs.org/power-assert) can only be imported using commonJS syntax
(ie. `const assert = require('power-assert')`), meaning it's treated as `any` by the compiler in many cases
## how to use
### normally
```ts
import assert from 'typed-nodejs-assert'
```
### with [power-assert](https://npmjs.org/power-assert)
since power-assert has to be imported using commonJS syntax, you have to import it as such, then give it the `PowerAsssert`
type from this package.
```ts
import { PowerAssert } from 'typed-nodejs-assert'
const assert: PowerAssert = require('power-assert')
```
**IMPORTANT:** you have to explicitly specify the type as shown above.
the following will **not work**:
```ts
const assert = require('power-assert') as PowerAssert
```
see [this issue](https://github.com/microsoft/TypeScript/issues/34596#issuecomment-691574987) for more info