class Song { kind : 'song' ; constructor ( public title , public duration ) { } } class Playlist { kind : 'playlist' ; constructor ( public name , public songs ) { } } function isSong ( item ) { return 'title' in item ; } function getItemName ( item ) { if ( item . kind === 'song' ) { return item . title ; } return item . name ; } const songName = getItemName ( new Song ( 'Wonderful Wonderful' , 300000 ) ) ; console . log ( 'Song name:' , songName ) ; const playlistName = getItemName ( new Playlist ( 'The Best Songs' , [ new Song ( 'The Man' , 300000 ) ] ) ) ; console . log ( 'Playlist name:' , playlistName ) ;