Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

False positive with 'unified-signatures' #2016

Closed
stephanedr opened this issue Jan 10, 2017 · 5 comments
Closed

False positive with 'unified-signatures' #2016

stephanedr opened this issue Jan 10, 2017 · 5 comments

Comments

@stephanedr
Copy link

Bug Report

  • TSLint version: 4.3.1
  • TypeScript version: 2.1.4
  • Running TSLint via: VSCode

TypeScript code being linted

function test(): void;
function test(a: number, b: number): void;
function test(a?: number, b?: number): void {}

Actual behavior

These overloads can be combined into one signature with an optional parameter. (unified-signatures)'
at: '3,27'

Expected behavior

No warning. I want to allow either 0 or 2 parameters.

@nchen63
Copy link
Contributor

nchen63 commented Jan 10, 2017

edit: never mind

Doesn't your 3rd signature allow 1 argument?

@nchen63
Copy link
Contributor

nchen63 commented Jan 10, 2017

@andy-ms, can you take a look?

@stephanedr
Copy link
Author

@nchen63 I considered that @andy-ms 's ticket title was answering your question.
In fact TS doesn't allow to call the implementation (function test(a?: number, b?: number): void {}), only the overload signatures can be called.

@davidwalschots
Copy link

I'm also failing to understand this rule, and I've therefore disabled it.

In the example below, I don't want to start merging overloads. For example, the second line has an optional country, but the third line doesn't, because it's required in stringified form by the messageFn. I don't want to merge these two as dictated by the rule, because that would allow not passing a country in that particular case.

export function postalCodeValidator(): ValidatorFn;
export function postalCodeValidator(message: string, country?: Country): ValidatorFn;
export function postalCodeValidator(messageFn: (country: string) => string, country: Country): ValidatorFn;
export function postalCodeValidator(message?: string | ((country: string) => string), country?: Country): ValidatorFn {

}

@ghost
Copy link

ghost commented Feb 28, 2017

The failure there is referring to the first two overloads. Here's a simpler example:

export function f(): void;
export function f(a: string, b?: string): void;
export function f(...args: any[]): void {

}

The function can be passed 0, 1, or 2 parameters. So it could be just f(a?: string, b?: string): void.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants