Help decipher weird TypeScript Return Type

I’ve been working with and using TypeScript for a couple of years now, but I am now just running into a type I just can’t understand and don’t know how to google…

The code snippet in question is a function that validates a password and looks like this:

export const validatePassword = (password?: string): password is string => {...}

What I don’t understand is the return type of this function password is string, what is this?? I know by inspecting the function logic that it should return a booolean but the function is doing more than checking if the password is string.

Can anyone explain this signature or provide link to the related TS docs?

You can read about it here.

I read that as “no matter what password is, treat it as a string”. Remember that according to your definition, password could be undefined. Now, you could deal with that in the code, but sometimes you can deal with it hear.

That’s just my understanding - I’m still learning TS myself.

1 Like

Thanks that makes more sense now :pray:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.