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

static toString with incorrect type breaks instanceof #12709

Closed
kieferrm opened this issue Dec 7, 2016 · 5 comments
Closed

static toString with incorrect type breaks instanceof #12709

kieferrm opened this issue Dec 7, 2016 · 5 comments
Assignees
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@kieferrm
Copy link
Member

kieferrm commented Dec 7, 2016

See microsoft/vscode#16819

TypeScript Version: typescript@2.1.4-insiders.20161206

  • VSCode Version: Code - Insiders 1.8.0-insider (0d65ced0fa7398dc9d50c39a23a2a8cc8406ff18, 2016-12-07T07:05:09.124Z)
  • OS Version: Darwin x64 15.6.0

Code

export abstract class Marker {
	static toString(markers: Marker[]): string {
		return '';
	}
}

export class Placeholder extends Marker {
	constructor(public name: string = '', public defaultValue: Marker[]) {
		super();
	}
}

function foo(marker: Marker) {
	if (marker instanceof Placeholder) {
		console.log(marker.name);
		console.log(marker.defaultValue);
	}
}

foo(null);

Expected behavior:
No erros are shown in the vscode editor

Actual behavior:
screen shot 2016-12-07 at 8 55 21 am

When the static toString is removed or renamed the problem goes away.

@mhegazy mhegazy added the Bug A bug in TypeScript label Dec 7, 2016
@sandersn sandersn changed the title static toString causing compile errors static toString breaks instanceof Dec 7, 2016
@sandersn sandersn changed the title static toString breaks instanceof static toString with incorrect type breaks instanceof Dec 7, 2016
@sandersn
Copy link
Member

sandersn commented Dec 7, 2016

  1. The type of toString is incorrect. markers is required and should be optional, because toString might get called on the class (not its instance). You can make markers optional as a workaround.
  2. We should probably relax the instance rhs requirement to just be a type that has a call or construct signature. It doesn't actually need to be assignable to Function.

-- OR --
2. Give an error when the instance or static side of a class incorrectly extends Object. But we don't do this today, and it would breaks tons of code. And people must not care too much, because I don't know of an existing bug for this.

@sandersn
Copy link
Member

sandersn commented Dec 7, 2016

Fix is up at #12728

@mhegazy mhegazy added Suggestion An idea for TypeScript and removed Bug A bug in TypeScript labels Dec 14, 2016
@mhegazy mhegazy added this to the TypeScript 2.2 milestone Dec 14, 2016
@about-code
Copy link
Contributor

Give an error when the instance or static side of a class incorrectly extends Object. But we don't do this today, and it would breaks tons of code. And people must not care too much, because I don't know of an existing bug for this.

Maybe there is: #442

@sandersn
Copy link
Member

As far as I can tell, #442 is essentially about using built-in names that are readonly, not incorrectly overriding a name that is meant to be overridden.

However, your PR to fix it looks undeservedly ignored. I'll take a look.

@about-code
Copy link
Contributor

As far as I can tell, #442 is essentially about using built-in names that are readonly, not incorrectly overriding a name that is meant to be overridden.

@sandersn indeed, you're right. I think I got this mixed up. Thank you very much for your encouraging review.

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Jan 9, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants