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

Infer type guards from instanceof and other such expressions #21956

Closed
chuckjaz opened this issue Feb 15, 2018 · 5 comments
Closed

Infer type guards from instanceof and other such expressions #21956

chuckjaz opened this issue Feb 15, 2018 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@chuckjaz
Copy link
Contributor

Consider the following from here:

const asAndBs = [new A(), new B(), new A(), new B()];
const allAs = asAndBs.filter(v => v instanceof A); 

The type of allAs is (A | B)[] where it would be more accurately typed as A[]. If a type-guard was used,

function isA(v: any): v is A {
    return v instanceof A;    
}

const allAs = asAndBs.filter(isA);

then the type would be A[] as expected.

Getting the correct type from filter requires using the verbose form filter(isA) instead of the more terse form filter(v => v instanceof A).

Suggestion

Infer a guard from lambdas that imply a type narrowing if used in an if statement.

For example,

const a = (v: any) => v instanceof Class;

would infer a to be of type (v: any) => v is Class.

const b = (v: any) => typeof v === 'string'

would infer b to be type (v: any) => v is string;

This would allow useful typing of:

const loweredStrings = someArray.filter(v => typeof v === 'string').map(v => v.toLower());
@jcalz
Copy link
Contributor

jcalz commented Feb 15, 2018

One of many duplicates of #5101. Did you try to search for related issues before posting this?

@chuckjaz
Copy link
Contributor Author

I tried. My search foo is clearly lacking.

@jcalz
Copy link
Contributor

jcalz commented Feb 15, 2018

Sure. I only ask because the issue posting template asks you to list what search terms you used... well, I guess it says that specifically for bug reports as opposed to suggestions.

I have this fantasy that one day we will find the magic wording that eliminates duplicate issues while still encouraging legitimate bug reports and suggestions. But sadly it is only a dream.

@aluanhaddad
Copy link
Contributor

@jcalz sophisticated lexical analysis should be employed by GitHub searches :)

@mhegazy mhegazy added the Duplicate An existing issue was already created label Feb 22, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 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

5 participants