You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was converting some of js files in React to ts when I faced this issue. I wanted to make some of props mandatory or optional based on value of a prop that is passed. The code in the component had destructed the variables from props causing this issue to occur.
Search Terms: destructuring
Code
Example:
typeErrorObj={isError: true,errorMessage: string,}|{isError:false,errorMessage?: string}constcheckError: (error: ErrorObj)=>void=(error)=>{error.isError&&logLength(error.errorMessage);}constcheckErrorAgain: (error: ErrorObj)=>void=({ isError, errorMessage })=>{isError&&logLength(errorMessage);// throws an error, Type 'undefined' is not assignable to type 'string'.}constlogLength=(message: string)=>console.log(message.length);
Expected behavior: Should correctly infer the type of errorMessage param
Actual behavior: Incorrect type being inferred and an ts error is thrown
Typescript version: 3.5.1
I was converting some of js files in React to ts when I faced this issue. I wanted to make some of props mandatory or optional based on value of a prop that is passed. The code in the component had destructed the variables from props causing this issue to occur.
Search Terms: destructuring
Code
Example:
Expected behavior: Should correctly infer the type of errorMessage param
Actual behavior: Incorrect type being inferred and an ts error is thrown
Playground Link: https://www.typescriptlang.org/play/index.html?jsx=2#code/C4TwDgpgBAogTnA9nA8gIwFZQLxQN4BQUUAlgM7xJwBcUwcArhADRFQQLICyEZZAhgHMItMvRIA7QawC+UAD75SFTjQBm-ADZkW7VTz5CIAflHipUGQQIBjRBLFQbACwg2A1pWS0AFByq0XqiYAJQ4AHxQAG6IJAAmOFB+qmHYkYTE-sgAdORBUABkBVCaiIIAMhBSwM7JVNlZcAYCwiEA3ARWdg7ATq4eQQCCgvySvo2BqugYqZEx8Yk+eMpBzHpUzUaWs-hseaqFxaUVVYI1ddy8LRDtnbb2jseV1c6LALZXRmZwkoI73WREJoINljj4PoZhKDTjVbkA
The text was updated successfully, but these errors were encountered: