Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to narrow a type in the same block scope?

I’m trying to narrow a type without introducing a new block scope:

type OptionalUser = Request & { user?: User }
type RequiredUser = Request & { user: User }

function handler(request: OptionalUser) {
  isAuthenticated(request) // adds user to request, throws if it can't add it
  request.user // request is now RequiredUser, same block scope as isAuthenticated
}

Is there a way to type isAuthenticated in such a way that the type of request is narrowed down in the same block scope of handler? I know I could use a function that returns a type predicate but then the narrowed type would only exist inside the block scope of an if statement. And if I were to have multiple functions like this it would cause a block scope hell.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

As caTS says it, assert functions are the way to go.

type User = {};

type OptionalUser = Request & { user?: User }
type RequiredUser = Request & { user: User }

function handler(request: OptionalUser) {
  isAuthenticated(request) // asserted
  request.user // User
}

function isAuthenticated(request: OptionalUser): asserts request is RequiredUser {
  if (request.user === undefined) {
    throw new Error("No user");
  }
}

Playground

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading