-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Array#includes check with enum #4246
Comments
Use: Why would you want to do the check otherwise? Are you receiving the value from a backend? |
that was actually a terrible example, sorry. a better function signature would be: function normalizeValue(item: string) {
if (options.includes(item)) {
return item;
}
return item.split(' ').join(',');
} I need to do different things based on whether the value is in the array or not, which is why I need to check. The value is 'kind of' user provided. If it's in the list, it's been set using a toggle, otherwise, it's been set using a text field. |
Ok, that makes sense. Not sure how to type this without casting It would be nice to have a clean solution here. It's not only |
this sounds like a bug to me (?), |
It feels like a tautological "explanation," but this doesn't work because includes(searchElement: T, fromIndex?: number): boolean; By the spec this is too restrictive: http://www.ecma-international.org/ecma-262/7.0/#sec-array.prototype.includes. That said, I prefer it this way because I'm certain that I can conjure up use cases that benefit from it as-is (and I prefer false negatives to false positives). Casting is the quick work around: function normalizeValue(item: string) {
if (((options:any):Array<string>).includes(item)) {
return item;
}
return item.split(' ').join(',');
} I like the following: type StringEnum = 'first_option' | 'second option' | 'third option'
const options = ['first_option', 'second option', 'third option'];
// Assert that `options` satisfies your old type, but don't impose the type on `options`
(options:Array<StringEnum>);
function normalizeValue(item: string) {
if ((options:Array<string>).includes(item)) {
return item;
}
return item.split(' ').join(',');
} |
@popham how would you fix something like https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVAXAngBwKZgDKGATgJYB2A5gKIUCuAtmALxgDkUZJAzhgPpwcGMnArswAHw488AYzEATQcNHipHDAAtuyoSLHsA3KgUU+YfWp6swAbU7c+Kg+IA0M+Updr2H9tq6PoYAuiaoUPQUcq5gFHAkjACGMGQAXngAain0eAAUZBh4jABcYHzk1ACUZcSVtAzMAN6oYGAkeBj0JBSWqmI8AHSUcjD0ing8BUWMVa1tYAD8YIXF821lVgN2AAxhqAC+QA?
Seems to me that this should work with or without any sort of casting. |
@ryanscottaudio it cannot work, because we cannot statically infer the contents of Here's kind of a workaround if you don't want casting: https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVAXAngBwKZgDKGATgJYB2A5gKIUCuAtmALxgDkUZJAzhgPpwcGMnArswAHw488AYzEATQcNHipHDAAtuyoSLHsA3KgUU+YfWp6swAbU7c+Kg+IA0M+Updr2H9tq6PoYAuiaoivIwAIYkBFD0FHKuYJRyMPSRPAAUZBh4jABcYHzk1ACUxQBGcHAweNEUYACkclryANY22ahgqfnMLEMcXLwCVoZS0nkFrMPssmZ6qpOS0wNzbAE6JMuu7Kjl6AlJKRRwJIzRMGQAXngAatf0eLkDxaWUVBrEZbQMjEqRFIXzoTDAAG9emA4hh6CQmmkMlk3gUjn0+gB+foFaF9YoTcx2AAMYVQAF8gA |
@niieani thanks for the workaround. what if it was a also would there be a way to do it WITH casting? I couldn't figure one out. |
@ryanscottaudio yeah, that should work. All of the |
@niieani it doesn't work with $ReadOnlyArray: https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVAXAngBwKZgDKGATgJYB2A5gKIUCuAtmALxgDkUZJAzhgPpwcGMnArswAHw488AYzEATQcNHipHDAAtuyoSLHsA3KgUU+YfWp4AuMABIASngCGigPIUYWAIIkSLlgAPMTk1HRMAHysYADanNx8KgbiADQy8krJauzp7Nq62YYAuiaoivIwLiQEUPQUcilglHIw9BU8ABRkGHiMdnxhVACUdgBGcHAwrhRgAKRyWvIA1jxgnahglqpiPAB0LW0d3b2Mw6jnqHUNTRRwJIwuMGQAXngAak-0eCd9A6SUKgaUKAiJnOwg8IMZgAb02YBqGHoJFmh3aeC6PT65y2WwA-M1TvCtnYrLtYgAGUqoAC+QA. I also don't get why your workaround works; what if options wasn't read-only and someone did |
@ryanscottaudio yes, I know it doesn't work with |
@niieani hahaha sorry, i thought you meant it should work if you put it in there. for the workaround though, |
I ran into exactly the same issue as in #4246 (comment) |
The type definition of Array.includes has been changed at some point and OP's code no longer errors. This issue can be closed. FWIW I think the original behaviour was better and the current type definition allows some easy to make mistakes to go unnoticed, as demonstrated in a similar TypeScript issue, but I guess that's a subject for another time and another issue. |
Hey,
So, I'm pretty sure that this will have a fairly logical solution, but I've been trawling through bugs and stack overflow and, I can't find a) a solution, and more importantly b) an explanantion.
I'm trying to validate that an item is part of an
enum
.However, in the
includes
call, flow gets mad becauseitem
is not a valid subset ofStringEnum
. Which I (probably) know, and I'm trying to check for.Is there a nice way to fix this?
Flow Try
The text was updated successfully, but these errors were encountered: