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

Class implementation ignores optionality of method parameter in interface #27523

Closed
maximgavrilov opened this issue Oct 3, 2018 · 4 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@maximgavrilov
Copy link

maximgavrilov commented Oct 3, 2018

TypeScript Version: 3.2.0-dev.201xxxxx

Search Terms: interface, optional parameter

Code

interface IRunner {
    run(s?: string): void;
}

class Runner implements IRunner {
    run(s: string): void {
        console.log(s.length);
    }
}

const runner: IRunner = new Runner();
runner.run(undefined);

Expected behavior:

Compilation error once strictNullChecks like TS2416: Type '(s: string) => void' is not assignable to type '(s?: string | undefined) => void'.

Actual behavior:

No compilation errors.

Playground Link

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Oct 3, 2018
@RyanCavanaugh
Copy link
Member

Things declared with method syntax are subject to parameter bivariance (see FAQ) due to the prevalence of (yes, unsound) class hierarchies in the wild that float their parameter types in the "wrong" direction

@sharwell
Copy link
Member

@RyanCavanaugh I'm assuming this is equivalent to the following example?

interface TestInterface {
	Method(text: string | undefined): void;
}

class TestClass implements TestInterface {
	public Method(text: string): void {
		console.log(text);
	}
}

I'm trying to find a way to validate these signatures at compile time. The implementation in my example has a bug; it should have been declared with the parameter text: string | undefined, but the omission of undefined can lead an implementer to make incorrect assumptions about the arguments.

@RyanCavanaugh
Copy link
Member

@sharwell correct. Unfortunately there's not a way to enable strict checks here (doing so would be trivial from an implementation perspective, but would immediately create errors in the DOM lib)

sharwell added a commit to sharwell/antlr4ts that referenced this issue Nov 28, 2018
The issue is not reported by the compiler. See microsoft/TypeScript#27523.
sharwell added a commit to sharwell/antlr4ts that referenced this issue Nov 29, 2018
The issue is not reported by the compiler. See microsoft/TypeScript#27523.
@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants