Typescript type inference, spread operator and multiple type return -


   interface skillproperty {         [name: string] : number      };      let skills: skillproperty;      skills = {}; // ok      skills = { fire: 123 }; // ok      skills = {         ...skills, // ok         ...{}, // ok         ...extraskills() // {} | { ice: number } not assignable type 'skillproperty'.     }      function extraskills() {         if (whatever) {             return {};         }         return { ice: 321 };     } 

how can change skillproperty interface make compliant both empty object , actual skillproperty type ?

your skillproperty interface is compatible {} | {ice: number}:

let noskills = {} let iceskills = { ice: 321 }; let randomskills: {} | {ice: number} = (math.random() < 0.5) ? noskills : iceskills let maybeskills: skillproperty = randomskills; // no error 

so, looks bug in typescript me. similar issue fixed, this case seems persist. might worthwhile opening new issue links existing ones.

in mean time there workarounds, like:

skills = {   ...skills, // ok   ...{}, // ok   ...extraskills() skillproperty // okay } 

hope helps; luck!


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -