2

I was wondering about the type guards in TypeScript, and if it is necessary to use them when only one type is defined in method signature. All examples in TypeScript docs only refer to situations when you have union type, e.g.:

myMethod(argument: string | number) {
 if (typeof argument === 'string') { // do my thing }
 if (typeof argument === 'number') { // do my thing }

But I've seen people using typeof when the type is strongly typed:

myMethod(argument: string) {
 if (typeof argument === 'string') { // do my thing }

Do you think it's a good approach? How do you check your data, especially the one that is not available during compile (e.g. from API endpoint)?

2 Answers 2

1

if code is like this

myMethod(argument: string) {

then you dont need to check type of using type, because in typescript you will get error if you do like this

let strVar : string;
strVar = 10;//error (means you cannot assign type other then string, same gose for method)

in angular you can set this to avoid conversion to any type by setting option in config

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [ "es2015", "dom" ],
    "noImplicitAny": true,//not allow implicit conversion to any type
    "suppressImplicitAnyIndexErrors": true
  }
}

Check here : https://angular.io/guide/typescript-configuration

Sign up to request clarification or add additional context in comments.

3 Comments

One caveat it that if your function is called from non-ts code then the ts-compiler will not check that obviously, so the runtime check might still make sense.
Theoretically that’s true, but when would that happen in a real-world scenario?
that's exactly what I meant, e.g. your API endpoint could send boolean instead of string, so if you don't check for type, you are risking runtime errors. Then the question is what is better, have the app fail or silently pass on errors...
0

In situations where I may not know what the object is and custom interfaces are being used use the keyword is and write a typeguard function.

For example I want to know if plant is a of type IFruit or IVegetable.

function isVeggie(plant: any): plant is IVeggie {
  return p.vegetable !== undefined;  // vegetable is a property on IVeggie
}
function isFruit(plant: any): plant is IFruit {
  return p.fruit !== undefined;  // fruit is a property on IFruit
}

Your type guards function should return true or false. I like to check for a given property on each type as an added check.

To use the type guards you will need to cast to any.

if (isVeggie(plant)) {
   plant as any as IVeggie;
   // Now you know plant is of type IVeggie
}

Unfortunately you have to assert toany before asserting to a custom interface (IE: IVeggie). So it's not quite as easy to use as some other languages.

Comments

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.