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

Type guards: support true & (maybe is X) #31021

Closed
5 tasks done
krisdages opened this issue Apr 18, 2019 · 4 comments
Closed
5 tasks done

Type guards: support true & (maybe is X) #31021

krisdages opened this issue Apr 18, 2019 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@krisdages
Copy link

krisdages commented Apr 18, 2019

Search Terms

type guards union assert

Suggestion

I'd like to be able to define a type guard's return type as true & (maybe is X)
and have any statements after calling the type guard function recognize that the value is of the correct type.

Technically false & (maybe is X) should probably be supported also if this is implemented, but that isn't in my particular use case.

Use Cases

The use case is an assertion helper

Assert.isType<T>(predicate <-- type guard for T, maybe: any, message: string): maybe is T {
   if (!predicate(maybe)) {
      throw Error(message);
   }
}

I'd like to be able to use maybe after the call since the Assertion will throw if the value is not of the right type.
.

Examples

Assert.isType<T>(predicate <-- type guard for T, maybe: any, message: string): true & (maybe is T) {
   if (!predicate(maybe)) {
      throw Error(message);
   }
}
...
function timesTwo(num: number) {
    Assert.isType(maybe => typeof maybe === "number", num, "Non-numeric value passed where number was expected."));

    return num * 2;
}

const val: any = "two";
console.log(timesTwo(val));	

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@MartinJohns
Copy link
Contributor

Duplicate of #17760.

@jcalz
Copy link
Contributor

jcalz commented Apr 18, 2019

Yeah, seems familiar 🙂

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Apr 18, 2019
@krisdages
Copy link
Author

@jcalz Thanks for pointing out your comment ... I saw that issue before I posted but I didn't see the syntax discussion below and for some reason the description didn't register as covering the same use case.

Sorry for the dupe. :)

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants