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

Add callable-types rule #1891

Merged
merged 1 commit into from Dec 17, 2016
Merged

Add callable-types rule #1891

merged 1 commit into from Dec 17, 2016

Conversation

ghost
Copy link

@ghost ghost commented Dec 16, 2016

PR checklist

What changes did you make?

Added the callable-types rule, which recommends to use a function type over an interface or type literal with just a call signature.

@nchen63 nchen63 merged commit 63fb778 into palantir:master Dec 17, 2016
@nchen63
Copy link
Contributor

nchen63 commented Dec 17, 2016

@andy-ms thanks!

@ghost ghost deleted the callable_types branch December 17, 2016 19:05
@PsySecCorp
Copy link

I've found having a module with common interfaces for callbacks improves code readability. For example:

export interface IError {
  (
    message: string,
    data: any,
  ): void;
}

export interface IDone {
  (
    error: any,
    data: any,
  ): void;
}

export interface INext {
  (
    error: any,
    data: any,
  ): void;
}

export interface IPassthru {
  (
    data: any,
  ): void;
}

This rule appears to prevent this. I'll be turning this rule off in my local tslint.json, but I'm curious what problem this rule is solving. Thanks!

@ghost
Copy link
Author

ghost commented Jul 19, 2017

You can just as easily use a type alias.

export type Error = (message: string, data: any) => void;

Also, in your example, if you used a specific function type instead of using a common one, you could avoid using any. You could also use a generic: export type Error<T> = (message: string, data: T) => void.

@PsySecCorp
Copy link

Oh my. Most helpful, @andy-ms Thanks for the breakdown!

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

Successfully merging this pull request may close these issues.

2 participants