1

Coming from Javascript I need some advice on how to do string validation in Typescript.

Usually in Javascript you can just check the string as if it was a boolean, but with strong types, you get a compiler error.

A couple of solutions I thought of, but don't really like is you could do !!myString or change the return type. Is checking for null, undefined and empty string the way to do it?

See example:

function stringIsValid(myString: String) : Boolean {
    return myString; // compiler error
}

var isValid = stringIsValid(null);

Playground

4
  • 1
    What does "valid string" mean? Non-null? Commented Jan 24, 2019 at 9:34
  • 1
    @ritaj In my case it's how Javascript would validate !!myString, so not null, undefined or empty. I validate strings like if(myString){...} everywhere, so I want to know how it's usually done in Typescript. Another example is in a switch case you can't just return the string if the return type is boolean. Commented Jan 24, 2019 at 9:41
  • No, you can't "just return the string if the return type is boolean". In JavaScript, a string is not a boolean. A truthy value acts like true but is not of type boolean. developer.mozilla.org/en-US/docs/Glossary/Truthy Commented Jan 24, 2019 at 9:57
  • This is even wrong in plain JS, semantically speaking... Commented Jan 24, 2019 at 13:33

2 Answers 2

3

You probably want to just use one of these, functionally they're the same:

function stringIsValid(myString: String): Boolean {
    return Boolean(myString);
}

or

function stringIsValid(myString: String): Boolean {
    return !!myString;
}
Sign up to request clarification or add additional context in comments.

6 Comments

I consider !!myString bad practise in Javascript, but I like the Boolean(myString) idea. Is it considered good practise?
Absolutely not. It is a bad practice to use a wrapper instead of a primitive value. An example from the Mozilla's documentation: var x = new Boolean(false); if (x) { /* this code IS executed */ }.
@Paleo Read more carefully. Boolean(value) returns a primitive, it's not wrapped. new Boolean(value) returns a Boolean wrapper object,
And the double NOT operator is a well-established practice in JavaScript and in TypeScript. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
@ritaj Ah yes my bad. The bad practice is with the constructor. Then you have to change the return type. Your function doesn't return a Boolean but a boolean.
|
3

The types from TS will not help you do runtime type validation on your variables, because TS only works at compile-time. There is a handy typeof command in JS to do type validation:

typeof myString === 'string'

The function you wrote

function stringIsValid(myString: string) : boolean {
    return myString;
}

will give you TS error at transpilation (compile-time), but these types will have no effect when you actually run your program. Here's an example of how to write it with proper typing and proper runtime check:

function stringIsValid(myString: string) : boolean {
    return typeof myString === 'string';
}

3 Comments

Using typeof is verbose in comparison to Javascript and will not return false if it's an empty string. Thanks for your explanation however :)
You can add more checks in order to cover all possible cases for checking that the string is "valid". I guess my point was that TypeScript will not help you with that. :)
Use primitive types (string, boolean) instead of wrapper types (String, Boolean) : typescriptlang.org/docs/handbook/declaration-files/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.