I slightly modified yourSkyShab's code, because it fired template change event on page load and it didn't fire when template was changed to default (since getEditedPostAttribute( 'template' ) is then '', which equals to null whanwhen testing for condition in your codetemplate change)
const { select, subscribe } = wp.data;
class PageTemplateSwitcher {
constructor() {
this.template = null;
}
init() {
subscribe( () => {
const newTemplate = select( 'core/editor' ).getEditedPostAttribute( 'template' );
if (newTemplate !== undefined && this.template === null) {
this.template = newTemplate;
}
if ( newTemplate !== undefined && newTemplate !== this.template ) {
this.template = newTemplate;
this.changeTemplate();
}
});
}
changeTemplate() {
// do your stuff here
console.log('template changed to ${this.template}');
}
}
new PageTemplateSwitcher().init();