Below is the code to detect whether a given object or a string is a string type object in TypeScript:
checkForString(obj: any) {
return obj instanceof String || typeof obj === 'string';
}
Examples of results based on above code:
this.checkForString('Hello World');
true
this.checkForString({});
false
this.checkForString({message:'Hello World'});
false