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
testArray.map(link => {
if (!link.subLinks) {
console.log('no sublinks!');
} else {
console.log('sublinks!');
// error
testRequiredArray = [...testRequiredArray, link];
// but this work as typescript knows that sublinks is defined and have at least one element
let test = link.subLinks[0];
}
});
🙁 Actual behavior
TypeScript doesn't detect in the "if" block that a value should be defined so it leads to an error when trying to use it for a Required variable.
I have the error: Type 'Link' is not assignable to type 'Required'.
Types of property 'subLinks' are incompatible.
Type '[Link, ...Link[]] | undefined' is not assignable to type '[Link, ...Link[]]'.
Type 'undefined' is not assignable to type '[Link, ...Link[]]'.ts(2322)
🙂 Expected behavior
TypeScript should detect that its defined.
Additional information about the issue
I used it with React but the same error appears with regular TypeScript code.
The text was updated successfully, but these errors were encountered:
🔎 Search Terms
"Required", "Undefined"
🕗 Version & Regression Information
⏯ Playground Link
https://playcode.io/2273403
💻 Code
interface Link {
path: string;
label: string;
subLinks?: [Link, ...Link[]]; // either undefined or defined with at least one element
}
let testRequiredArray: Required[] = [];
const testArray: Link[] = [
{ path: '1', label: '1' },
{ path: '2', label: '2', subLinks: [{ path: '3', label: '3' }] },
];
testArray.map(link => {
if (!link.subLinks) {
console.log('no sublinks!');
} else {
console.log('sublinks!');
// error
testRequiredArray = [...testRequiredArray, link];
// but this work as typescript knows that sublinks is defined and have at least one element
let test = link.subLinks[0];
}
});
🙁 Actual behavior
TypeScript doesn't detect in the "if" block that a value should be defined so it leads to an error when trying to use it for a Required variable.
I have the error: Type 'Link' is not assignable to type 'Required'.
Types of property 'subLinks' are incompatible.
Type '[Link, ...Link[]] | undefined' is not assignable to type '[Link, ...Link[]]'.
Type 'undefined' is not assignable to type '[Link, ...Link[]]'.ts(2322)
🙂 Expected behavior
TypeScript should detect that its defined.
Additional information about the issue
I used it with React but the same error appears with regular TypeScript code.
The text was updated successfully, but these errors were encountered: