Skip to content

DetachHead/typed-nodejs-assert

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

typed nodejs assert

various fixes for nodeJS assertions in typescript.

npm

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, 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:

    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 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

import assert from 'typed-nodejs-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.

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:

const assert = require('power-assert') as PowerAssert

see this issue for more info

About

various fixes for nodeJS assertions in typescript.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published