microsoft / microsoft/TypeScript
`asserts` keyword does not enhance hoisted function type, works on functions assigned to a variable
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked TypeScript Playground reproduction and compare the hoisted function a with the variable-assigned functions b and c after assignLabel is called. Trace the type-checker behavior for assertion functions and confirm completion when a.label is accepted in code after the call without regressing the working cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100