Skip to content

Assertion functions don't work with wildcard imports #35004

@dfilatov

Description

@dfilatov

TypeScript Version: 3.7.2

Search Terms:

assert function import export

Code

// Module asserts.ts
function isNonNullable<T>(obj: T): asserts obj is NonNullable<T> {
    if(obj === undefined || obj === null) {
        throw new Error('Must not be a nullable value');
    }
}

export {
    isNonNullable
};

// Module test.ts
import * as asserts from './asserts.ts';

function test(obj: string | null): void {
    asserts.isNonNullable(obj);
    obj.trim();
}

Expected behavior:
Everything works as expected

Actual behavior:
Error: Assertions require every name in the call target to be declared with an explicit type annotation.

If I rewrite test.ts with:

// Module test.ts
import { isNonNullable } from './asserts.ts';

function test(obj: string | null): void {
    isNonNullable(obj);
    obj.trim();
}

then everything compiles without any error

Metadata

Metadata

Assignees

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