![]() ![]() This would be even more unnatural as we'd essentially intentionally be performing a cast that we know may be inaccurate - however, it would have the benefit that the error above would have been averted. Note: To work around the initial TS error, I could have used a cast instead of an explicit type-check. However, I think in this particular circumstance, it's not facilitating correctness or behaving how developers would expect. Generally TypeScript doesn't get in the way, for the most part it encourages correctness - honestly it's great. It was never the developer's intention to check that input was a string and a known value the developer simply wanted to check whether input was a known value - but wasn't permitted to do so. It caused us to change our code from what we intended to what it allowed. It's because TypeScript's type system got in the way of the developer's initial intent. Why is that? Where is the breakdown here? Where as our original "naive" approach would have worked just fine. This TypeScript compiles without errors, but it's not correct. freeze ( ) function foo ( input?: string | null ) ![]() Given the function was designed and documented to have this behaviour, is there any particular reason why TypeScript won't allow us to utilize this function as documented?Īt least based on my current understanding, it seems unusual to me that.:Ĭonst KNOWN_VALUES = Object. If Type(x) is different from Type(y), return false.Īs such includes offers behavior specifically guaranteed to return false if the type of searchElement matches no elements in the array. Somewhat to the contrary, the first step of the aforementioned SameValueZero algorithm is: There's no indication this function should not be called with types that do not exist in the array. If the computed index is less than 0, the whole array will be searched. If it is negative, it is used as the offset from the end of the array to compute fromIndex. If it is greater than or equal to the length of the array, false is returned, i.e. The optional second argument fromIndex defaults to 0 (i.e. Includes compares searchElement to the elements of the array, in ascending order, using the SameValueZero algorithm, and if found at any position, returns true otherwise, false is returned. Includes ( searchElement: any, fromIndex?: number ): boolean ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |