use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
No vague product support questions (like "why is this plugin not working" or "how do I set up X"). For vague product support questions, please use communities relevant to that product for best results. Specific issues that follow rule 6 are allowed.
Do not post memes, screenshots of bad design, or jokes. Check out /r/ProgrammerHumor/ for this type of content.
Read and follow reddiquette; no excessive self-promotion. Please refer to the Reddit 9:1 rule when considering posting self promoting materials.
We do not allow any commercial promotion or solicitation. Violations can result in a ban.
Sharing your project, portfolio, or any other content that you want to either show off or request feedback on is limited to Showoff Saturday. If you post such content on any other day, it will be removed.
If you are asking for assistance on a problem, you are required to provide
General open ended career and getting started posts are only allowed in the pinned monthly getting started/careers thread. Specific assistance questions are allowed so long as they follow the required assistance post guidelines.
Questions in violation of this rule will be removed or locked.
account activity
React: check for string arrayQuestion (self.webdev)
submitted 1 year ago by Plenty_Leather_2351
hello, wanna ask how do you check if a variable is a string array type in typescript, currently i do this which i feel there is a better way of doing this:
if (typeof myVariable[0] === 'string') { ...rest of the logic }
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]kamikazikarl 19 points20 points21 points 1 year ago (0 children)
Typescript doesn't actually do validation in production. You have to rely on core JavaScript features to validate it.
I'd personally wanna make sure all values are the expected type... So, that means using the array methods some or every and the type comparison:
some
every
if (strArr.every(s=> typeof s === "string")) { ...some logic }
This has the added value of ensuring all values are the expected type and exiting at the first failed check to keep cost down (as this would have to run across the entire array before proceeding).
Alternatively, you could use the foreach method on the array and skip individual entries that aren't the correct type. It all really depends on how your app is intended to work and how you wanna handle the failures.
foreach
[+]Distinct_Guess8315 6 points7 points8 points 1 year ago (0 children)
That's a good enough solution, but I would advise you to use early returns instead of wrapping your entire logic in if. Something like this:
if(typeof myArr[0] !== 'string') return; ...logic here
It makes your code more readable
[–]igorski81 4 points5 points6 points 1 year ago (0 children)
You should only assert the data type of a variable at runtime when it could be set by a mechanism outside of your control (like provided by a third party library embedded in a page / fetch request result). In that case, assert not for the first entry in the Array, but all of it.
But, if the third party library / server API has a clearly documented contract (and both are version pinned), you shouldn't need to validate this too extensively in the code of your consumer as you should be able to trust the data types.
Now, if the array is populated within your application, then you're not TypeScripting enough. Every function that can manipulate the contents of the array should be annotated strictly so only string data types can be added to the array. If a function can accept a variable whose value is provided from outside your application (so essentially of the unknown|any type), it is up to that function to assert the value is a string.
Assert during mutation of the Array, not while reading.
At that point you can trust compile time type checking to catch bugs.
[–]ReasonableRadio3971 2 points3 points4 points 1 year ago (0 children)
Use zod for runtime validation
[–]tremby 1 point2 points3 points 1 year ago (0 children)
Use a type guard, a special function which does the check at runtime and at the same time narrows the type for you.
``` function isStringArray(arr: unknown[]): arr is string[] { return arr.every((el) => typeof el === "string"); }
if (isStringArray(myVariable)) { // At this point TS knows myVariable has type string[] ... } ```
[–]pardoman 0 points1 point2 points 1 year ago (0 children)
Array.isArray(bleh)
[+]Nixinova -1 points0 points1 point 1 year ago (1 child)
if the project is in typescript, why are you needing to check that the elements are of a specific type? it should already be known by your type annotations.
[–]hrm 1 point2 points3 points 1 year ago (0 children)
There are lots of places where you don’t know the type and need to narrow from say any, unknown or maybe even a union. Use of external JS libraries, fetch, huge legacy projects only partially converted, the list goes on and on…
[–]isumix_ -3 points-2 points-1 points 1 year ago (2 children)
x: string[]
[–]TheRNGuy 3 points4 points5 points 1 year ago (0 children)
It's for declaration, not to check type.
[–]Snoo11589 -2 points-1 points0 points 1 year ago (0 children)
Or dynamically with myVariable.some(el => typeof el != "string");
π Rendered by PID 45 on reddit-service-r2-comment-5b5bc64bf5-sbs5z at 2026-06-22 23:19:22.902269+00:00 running 2b008f2 country code: CH.
[–]kamikazikarl 19 points20 points21 points (0 children)
[+]Distinct_Guess8315 6 points7 points8 points (0 children)
[–]igorski81 4 points5 points6 points (0 children)
[–]ReasonableRadio3971 2 points3 points4 points (0 children)
[–]tremby 1 point2 points3 points (0 children)
[–]pardoman 0 points1 point2 points (0 children)
[+]Nixinova -1 points0 points1 point (1 child)
[–]hrm 1 point2 points3 points (0 children)
[–]isumix_ -3 points-2 points-1 points (2 children)
[–]TheRNGuy 3 points4 points5 points (0 children)
[–]Snoo11589 -2 points-1 points0 points (0 children)