TypeScript check for empty string

I believe this is as close as you’re going to get only using the typing system (rather than having a ‘nonEmptyString’ class)

type nonEmptyString = never; // Cannot be implicitly cast to
function isNonEmptyString(str: string): str is nonEmptyString {
    return str && str.length > 0; // Or any other logic, removing whitespace, etc.
}

Testing it:

let fn = function(a: nonEmptyString) {

}

let someStr="";
if (isNonEmptyString(someStr)) {
    fn(someStr); // Valid
} else {
    fn(someStr); // Compile error
}

Unfortunately, you end up with warts since nonEmptyString is never. Which means you need to explicitly cast nonEmptyString back to string.

let fn = function(a: nonEmptyString) {
    let len = a.length; // Invalid
    let len2 = (<string>a).length; // Valid
    let str = a + 'something else'; // Valid (str is now typed as string)
}

One possible resolution is:

type nonEmptyString = string & { __nonEmptyStr: never };

Which alleviates the problem of having to explicitly cast back to a string (all three tests above are valid), but does pollute the type with __nonEmptyStr (which will be undefined if referenced).

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)