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
Thanks, sorry for not catching the existing issue.
I still wonder how the compile error arrives since ArrayClass is explicitly declared to be of type TypedArrayConstructor..somehow the assignment still changes its type?
I still wonder how the compile error arrives since ArrayClass is explicitly declared to be of type TypedArrayConstructor..somehow the assignment still changes its type?
Assignment narrows the type. Here's a simplified example:
constvalue: string|number=123;// Always assigning a number.value;// Variable is typed number, because the compiler knows we always assign a number.
In your Ok case the variable is narrowed to Int8Array, because the compiler can see that's what you always assign. In your error case the compiler can't know it, so it can't narrow the variable.
I still wonder how the compile error arrives since ArrayClass is explicitly declared to be of type TypedArrayConstructor..somehow the assignment still changes its type?
Assignment narrows the type. Here's a simplified example:
const value: string | number = 123; // Always assigning a number.
value; // Variable is typed number, because the compiler knows we always assign a number.
In your Ok case the variable is narrowed to Int8Array, because the compiler can see that's what you always assign. In your error case the compiler can't know it, so it can't narrow the variable.
Oh, I wasn't aware of that, interesting. I can see this being useful in many cases, I could also argue that if the developer declares a type explicitly then no further automatic narrowing should be done..I'm guessing the reason it still happens is that it can only help catch more errors, so there is no advantage not doing it.
🔎 Search Terms
When creating a class for a typed array, if this class is returned by indexing into an object, I get a compile error.
See attached playground link.
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play/?#code/KYDwDg9gTgLgBDAnmYcAqzgBMCCUoCGiAwhAHYDOMUArgMYzRwC8AUAJAA+cAqgJZkYADjyES5KrQbQO3foICMANlFFSlavUZRZvATADMAJlXiNU7boCSgkfjUTN0nVzg2Yy0+slaZr98ZejhZ+3ABiADYQBIYm9mY+zqysoJCwcHSOcN4wBIIULHAA3qxwcABCAJpoAKIAXHAArApGAAwANKW8AHIAylYA4t01ACIA+lW1Dc1GCp1lvQASAPIASmjTLUbzPf1Do2NLaxtNLQY7Vt0nMwAsnew8fYPD45fXLY2dHGEAMss47yMSk6AF9kmVUtB4JkNHACPFyogMChCiV2ABtHJ5GAUAB0kxqAF0yiTSWUGu47GJ7pjHNi8Y89i8JtUieS9LZTDSsflcUd1sSyaSKYoVPFuXTeYzngd+WhiQ15B4xdSOLSNPTcW9BULhW5BIFxWqeTjcdL9q8rjrFfpDaqMSa8b9-vLdSSGpForEuawQXCCjCqABuZKsQPwABGNAAZtHgFBCmRgAB3OCmcoxuNQAAUAEoQ6wAPSFuDLADWrBKZXDafixAiBAoFAayOwQXMvgTzH1wlMIerWToiZTtbE9cbFGzUdj8facA6cAU+d9yWL2QgAFswHwIqgavgZFWMoPN5Ak4JW4VHfjWf3j7CvA2my3MLg68FO4V4WJEa30Zkt3IYAL0wQk7xrIduyTVNHwnKdM1nec5yXIM4DXGpwGABhsHnABaBQ4SgABzGgN2AnEV2SIA
💻 Code
🙁 Actual behavior
The last line returns a compile error.
🙂 Expected behavior
There should be no compile errors.
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: