1.

Explain how tuple destructuring works in TypeScript.

Answer»

You can destructure tuple elements by USING the assignment operator (=). The DESTRUCTURING variables get the types of the corresponding tuple elements.  

let employeeRecord: [string, number] = ["JOHN Doe", 50000];let [emp_name, emp_salary] = employeeRecord;console.log(`Name: ${emp_name}`); // "Name: John Doe"console.log(`Salary: ${emp_salary}`); // "Salary: 50000"

After destructuring, you can’t assign a VALUE of a different type to the destructured VARIABLE. For example,

emp_name = true; // Type 'boolean' is not assignable to type 'string'.(2322)


Discussion

No Comment Found