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

Overloading on boolean constants #4965

Closed
pimterry opened this issue Sep 25, 2015 · 2 comments
Closed

Overloading on boolean constants #4965

pimterry opened this issue Sep 25, 2015 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@pimterry
Copy link
Contributor

Currently (as of TypeScript 0.9), TypeScript supports overloading on constants, but string constants only.

This seems like a slightly arbitrary restriction, although I do agree that strings are the most obvious case where this is necessary. I do think there is (at least) one more case that comes up frequently though: booleans.

Motivation

My current motiviating example for this is in Lodash's _.flatten function: https://lodash.com/docs#flatten

This takes a boolean 2nd argument; a flag which changes the behaviour of the method slightly. If true, it recursively flattens, and if false it only flattens a single level.

Lodash also has _.flatten / _.flattenDeep methods without boolean arguments that follow each of these behaviours respectively without the flag. While I think we can agree that generally using the different methods where possible is often better, this still gets a lot of use, and boolean flags for this sort of thing are pretty common generally.

Notably, these two (recursive/non-recursive) behaviours do have different type signatures, as you can see in the DefinitelyTyped definitions of all three different methods (https://github.com/borisyankov/DefinitelyTyped/blob/master/lodash/lodash.d.ts#L848-L881). The method with the boolean signature though has to be overly general to cover both cases, even though when the flag passed is constant we can easily be more specific at compile time.

Design

Exactly the same as for string constants (https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.9.2.4), but with true/false values.

See below for examples:

interface MyClass {  
    serialize(x: true): string;   
    serialize(x: false): number;  
    serialize(x: boolean): string | number;  
}

var obj: MyClass;
var aBoolean: boolean = Math.random() >= 0.5;

// Valid:
var x: string = obj.serialize(true);
var y: number = obj.serialize(false);
var z: string|number = obj.serialize(aBoolean);

// Not valid:
var a: number = obj.serialize(true);
var b: string = obj.serialize(false)

var c: number = obj.serialize(aBoolean);
var d: string = obj.serialize(aBoolean);
@yortus
Copy link
Contributor

yortus commented Sep 25, 2015

This would be covered more generally by 'singleton types', which have been discussed for strings on #1003, and for booleans on #2873 (comment).

Singleton types (at least for strings so far) have been approved according to #2936 but things have gone quiet about the concept for a quite a while, and they are not in the roadmap. Would be nice to get an update from someone on the team about where this is at.

@DanielRosenwasser
Copy link
Member

Hey @yortus I'm going to be investigating string types for this release.

In the mean time, I'm closing this issue as a duplicate of the above. Thanks for the links!

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Sep 25, 2015
@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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants