Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assertion functions don't work with wildcard imports #35004

Closed
dfilatov opened this issue Nov 8, 2019 · 7 comments · Fixed by #51324
Closed

Assertion functions don't work with wildcard imports #35004

dfilatov opened this issue Nov 8, 2019 · 7 comments · Fixed by #51324
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Milestone

Comments

@dfilatov
Copy link

dfilatov commented Nov 8, 2019

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

@ilogico
Copy link

ilogico commented Nov 10, 2019

To reproduce this issue, you have to use namespace imports import * as assert from './assert.js' and export the function through an export list export { check }.
When the function is exported with export function check(..., everything works correctly.

@andrewbranch andrewbranch added the Bug A bug in TypeScript label Dec 6, 2019
@andrewbranch
Copy link
Member

I seem to recall that a lot of cleverness was required to support tracking these assertion signatures through property access without performance regressions, so this isn’t super surprising... But since export function isNonNullable ... does work (thanks @ilogico), I’m hopeful this a simple fix.

@G-Rath
Copy link

G-Rath commented Mar 3, 2020

I believe this issue is preventing shipping assert guards in @types/node, due to @types/adone containing an std object that does:

import assert = require("assert");
...

export {
  assert,
  ...
}

I'm going to look further into this, especially since adone is deprecated, so might be able to hack around it, but if this is fixable that would help too

This is the PR if people want to take a look: DefinitelyTyped/DefinitelyTyped#42786

@dfilatov
Copy link
Author

@RyanCavanaugh Is there any plan to fix this?

@RyanCavanaugh
Copy link
Member

@dfilatov eventually, yes, or if someone wants to send a PR, that'd be great

@jakebailey
Copy link
Member

Here is a similar case which affects the module-ified TS compiler:

// @strict: true

// @filename: src/core/_namespaces/ts.ts
import * as Debug from "../debug";
export { Debug };

// @filename: src/core/debug.ts
export declare function assert(expression: unknown): asserts expression;


// @filename: src/core/foo.ts
import * as ts from "./_namespaces/ts";
import { Debug } from "./_namespaces/ts";

ts.Debug.assert(true);
Debug.assert(true);


// @filename: src/other/_namespaces/ts.ts
export * from "../../core/_namespaces/ts"


// @filename: src/other/bar.ts
import * as ts from "./_namespaces/ts";
import { Debug } from "./_namespaces/ts";

ts.Debug.assert(true);
Debug.assert(true);

ts.Debug.assert doesn't work, but Debug.assert does. That's curious because the latter is still a wildcard import, just not the "top level" one. Using the "top level" one is where it breaks, but going a little more direct works.

@jakebailey
Copy link
Member

This turned out to be really simple; will send a PR for it soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants