Code
// @filename: declares.d.ts
// this is the declaration for 'assert' from @types/node
export function art(value: any, message?: string | Error): asserts value;
// @filename: test.js
const { art } = require('./declares')
let x = 1
art(x)
Expected behavior:
No error.
Actual behavior:
Error: "Assertions require every name in the call target to be declared with an explicit type annotation."
There is no error when using ES imports:
// @filename: test2.js
import { art } from './declares'
let x = 1
art(x)
Repros since 3.7, when we introduced asserts.
My hot take is that this is a bogus error almost everywhere you could get it...
I ran into this today when updating @types/node and using require('assert') from node core.
I fixed this with
/** @type {import('assert')} */
const assert = require('assert')
Is it possible for the type definition of require in TypeScript itself to return function require(module: T): import<T> ?
The problem is that symbols imported via require aren't actually aliases, so they don't get resolved in getTypeOfDottedName.
I prototyped a solution at the branch alias-for-require. It fixes this example, but is woefully incomplete everywhere else. It still needs:
@weswigham you might be interested in this.
Most helpful comment
I ran into this today when updating
@types/nodeand usingrequire('assert')from node core.I fixed this with
Is it possible for the type definition of
requirein TypeScript itself to returnfunction require(module: T): import<T>?