Skip to content

Add an "argument is not type" operator #16812

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

Closed
Arlen22 opened this issue Jun 28, 2017 · 9 comments
Closed

Add an "argument is not type" operator #16812

Arlen22 opened this issue Jun 28, 2017 · 9 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@Arlen22
Copy link

Arlen22 commented Jun 28, 2017

We already have:

function isNotUndefined(obj:any): obj is string | number | boolean | MyCustomType | null | MyOtherCustomType {
    return typeof obj !== "undefined";
}

Now we just need:

function isNotUndefined(obj:any): obj is not undefined { // or isnot
    return typeof obj !== "undefined";
}
@kitsonk
Copy link
Contributor

kitsonk commented Jun 28, 2017

Dupe (or heavily related to) #4183

@RyanCavanaugh
Copy link
Member

function isNotUndefined<T>(obj: T | undefined): obj is T {
    return typeof obj !== "undefined";
}
var x = Math.random() > 0.5 ? undefined : 32;
if (isNotUndefined(x)) {
    console.log(x.toFixed());
}

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Jun 28, 2017
@Arlen22
Copy link
Author

Arlen22 commented Jun 28, 2017

const test: (string | undefined)[] = []
test.filter(<T>(a: T): a is T | undefined => { 
    return !!a; 
}).map(t => {
    // t is string | undefined
})

@RyanCavanaugh
Copy link
Member

@Arlen22 why did you write the predicate wrong? This works in 2.4:

const test: (string | undefined)[] = []
test.filter(<T>(a: T | undefined): a is T => { 
    return !!a; 
}).map(t => {
    t;
    // t is string
})

@Arlen22
Copy link
Author

Arlen22 commented Jun 29, 2017

Strange. In 2.4.1:

const test: (string | undefined)[] = []
test.filter(<T>(a: T | undefined): a is T => { 
    return !!a; 
}).map(t => {
    let r: string = t; //Error: Type 'string | undefined' is not assignable to type 'string'
})

@Arlen22
Copy link
Author

Arlen22 commented Jun 29, 2017

Nevermind, I forgot to update my global tsc. Now I'm seeing the new feature. Nice :)

@Arlen22
Copy link
Author

Arlen22 commented Jun 29, 2017

Any idea what PR brought this in?

@RyanCavanaugh
Copy link
Member

#11858

@mhegazy
Copy link
Contributor

mhegazy commented Aug 17, 2017

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

@mhegazy mhegazy closed this as completed Aug 17, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants