Microsoft/TypeScript

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

Open

#41 232 ouverte le 25 oct. 2020

Voir sur GitHub
 (3 commentaires) (1 réaction) (0 assignés)TypeScript (6 726 forks)batch import
BugDomain: check: Control FlowHelp Wanted

Métriques du dépôt

Stars
 (48 455 stars)
Métriques de merge PR
 (Merge moyen 6j 17h) (9 PRs mergées en 30 j)

Description

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

Guide contributeur