I'm working with this code snippet:
interface MyInterface {
name: string;
}
type MyType = string | MyInterface;
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
constructor() {
console.log(this.getValueAsString('Hello World'));
console.log(this.getValueAsString({name: 'Hello World'}));
}
// I need to return 'Hello World' regardless of the type
getValueAsString(myValue: MyType): string {
// current implementation is causing compiler errors
// return myValue.name ? myValue.name : myValue;
}
}
The issue lies in resolving the type mismatch error for the function getValueAsString
.
What would be the optimal solution for handling getValueAsString
?
(stackblitz: https://stackblitz.com/edit/angular-jrty3q)