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

asserts keyword does not enhance hoisted function type, works on functions assigned to a variable #41232

Open
phryneas opened this issue Oct 25, 2020 · 2 comments · May be fixed by #58411
Open
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@phryneas
Copy link

phryneas commented Oct 25, 2020

TypeScript Version: 4.0.2, 4.1.0-beta

Search Terms:
asserts function

Code

interface LabelledFunction { label: string; }

function assignLabel(fn: () => unknown, label: string): asserts fn is LabelledFunction {
  Object.assign(fn, { label });
}

function a() { }
assignLabel(a, 'a');
console.info(a.label); // Property 'label' does not exist on type '() => void'.(2339)

const b = function () { };
assignLabel(b, 'b');
console.info(b.label); // works as expected

const c = () => { };
assignLabel(c, 'c');
console.info(c.label); // works as expected

Expected behavior:

At least in runtime code coming after assignLabel(a, 'a');, a.label should be accessible.

Actual behavior:

Property 'label' does not exist on type '() => void'.(2339)

Playground Link: link

@phryneas phryneas changed the title asserts keyword does not enhance hoisting function type, works on functions assigned to a variable asserts keyword does not enhance hoisted function type, works on functions assigned to a variable Oct 25, 2020
@robbiespeed
Copy link

Same thing happens to classes:

interface Labelled {
  label: string;
}

function assignLabel <T> (value: T, label: string): asserts value is (T & Labelled) {
  Object.assign(value, { label });
}

function a() { }
assignLabel(a, 'a');
console.info(a.label); // Property 'label' does not exist on type '() => void'.(2339)

const b = function b () { };
assignLabel(b, 'b');
console.info(b.label); // Works

class X {}
assignLabel(X, 'X');
X.label; // Property 'label' does not exist on type '() => void'.(2339)

const Y = class Y {}
assignLabel(Y, 'Y');  // Works
Y.label;

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Nov 4, 2020
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Nov 4, 2020
@denismaxim0v
Copy link

I think I could look into it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants