Skip to content

In JS, assertions imported via commonjs have bogus error #38379

Closed

Description

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)

Activity

  1. sandersn commented on May 6, 2020

    @sandersn
    MemberAuthor

    Repros since 3.7, when we introduced asserts.

  2. DanielRosenwasser commented on May 7, 2020

    @DanielRosenwasser
    Member

    My hot take is that this is a bogus error almost everywhere you could get it...

  3. Raynos commented on Jun 15, 2020

    @Raynos

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

  4. sandersn commented on Jul 8, 2020

    @sandersn
    MemberAuthor

    The problem is that symbols imported via require aren't actually aliases, so they don't get resolved in getTypeOfDottedName.

  5. sandersn commented on Jul 9, 2020

    @sandersn
    MemberAuthor

    I prototyped a solution at the branch alias-for-require. It fixes this example, but is woefully incomplete everywhere else. It still needs:

    1. More complete handling of object literal syntax, which has way more options than the import syntax.
    2. Handling of un-destructured require.
    3. Rewrite of d.ts emit code.
    4. Replacement and removal of all the special-case fake-alias code elsewhere.
    5. Anything else I don't know about right now.

    Wesley Wigham (@weswigham) you might be interested in this.

  6. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions