microsoft / microsoft/TypeScript

ES6 Class: `asserts this is Type` raises error when variable containing instance is type narrowed

Open
#54,591 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: This-Typing Help Wanted Possible Improvement
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Bug Report

🔎 Search Terms
  • Error: "Assertions require every name in the call target to be declared with an explicit type annotation"
  • Assertation function as class method
  • Type narrowing class and then calling assertation method
🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about this
⏯ Playground Link

Playground link with relevant code

💻 Code
class MyClass {
	public assertIsMyClass(): asserts this is MyClass {
		if (!(this instanceof MyClass)) {
			throw new Error('this was not of type MyClass');
		}
	}
}

const maybeMyClass: MyClass | undefined = new MyClass();

if (maybeMyClass) {
	// This fails with error:
	// Assertions require every name in the call target to be declared with an explicit type annotation.(2775)
	maybeMyClass.assertIsMyClass();

	// But if we simply assign to a new variable without the optionality, it's fine:

	// this line works because we have narrowed the type through the if statement above
	const definitelyMyClass: MyClass = maybeMyClass; 
	// This raises no error even though it's equivalent to the first attempt
	definitelyMyClass.assertIsMyClass();
}
🙁 Actual behavior

Calling the class method assertation function raises error: "Assertions require every name in the call target to be declared with an explicit type annotation.(2775)"

🙂 Expected behavior

It should pass validation and narrow to the type given in the assertation.
This is because it will have this behavior if you simply assign to a new variable with a new type annotation (but which is equivalent to the narrowed type at that point).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied Playground link and reproduce the diagnostic in the code sample, comparing the narrowed variable with the explicitly annotated variable. Trace the assertion-call validation in the TypeScript compiler and add coverage for this class-method case. Done means the narrowed call passes validation while existing assertion checks remain correct.

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
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.