microsoft / microsoft/TypeScript

Cryptic error message without --noImplicitAny

Open
#41,117 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: Error Messages
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 4.1.0-dev.20201015

Search Terms: noImplicitAny

In the JS mode given the class inheritance hierarchy A -> B -> C, when B code refers to class declaration (e.g. in a clone() method) and the class is assigned to an object property (NS.B = class ...), an attempt to convert an instance of C to B returns a cryptic error message: Property <property name> is missing in type 'C' but required in type 'B'.

Expected behavior: This diagnostic isn't helping, maybe omit it?

Related Issues:

Code

class A {
	clone() {
		return new A();
	}
}

var NS = {};

NS.B = class extends A {
	constructor(v) {
		super();
		this.val = v;
	}

	//** @returns {NS.B} */  // this jsdoc comment fixes the error
	clone() {
		return new NS.B(this.val);
	}
}

class C extends NS.B {
	constructor() {
		super(1);
	}

	clone() {
		return new C();
	}
}


/** @returns {NS.B} */
function f() {
	return new C(); // error here: Property 'val' is missing in type 'C' but required in type 'B'.
}
Compiler Options
{
  "compilerOptions": {
    "alwaysStrict": true,
    "esModuleInterop": true,
    "checkJs": true,
    "allowJs": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "Latest",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

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

Reproduce the example from the issue or its TypeScript Playground link with checkJs and the shown JavaScript class hierarchy. Start by tracing the type-checking path for the return in f() and the inferred type of NS.B. Done means the diagnostic is removed or made useful without requiring the JSDoc workaround, with the behavior covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.