how come this returns undefined
The type of
partPresignedUrl
is:
Ie. its an array of objects.
The code for the image:
this really shouldn't be any different from this https://www.typescriptlang.org/play?#code/MYewdgzgLgBAtgTwIICcUwLwwNoG8YCuKANgFwwBEAjNISRQDQwAOAhilAHIFwBGApinJUYAXyb4iZSgCYwAEzrFGLdlx4ChMGWIlLyFAMwpFUlWw7c+g8obEBdANwAoZwDMQ6ABShIsSSRMFurW6KIwIG7wyGgAlDC4zjDJML4QIMT8AHTEIADmXlJBalaasc6iQATS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
33 Replies
Not undefined for me
if you are talking about the ts-playground link, yeah that one behaves as expected/wanted, but the console.logs in the image are the ones I dont get
it shows the values if I log the the whole array, but returns undefined if I try to select the values of a specific object
its a little tough cause you didnt paste the actual code youre running
cause its probably something small
the code blocks is the code that I am running, but maybe I should create a codesandbox
but at the same time I doubt that something RHF or useCallback is the culprit
uhh i qactually think it might be
RHF does a bunch of very complicated things to get the typings to work
for instance
this is from my code
almost all of those fields are actually nonoptional, but they do a bunch of weird stuff so every field (and every subfield) in the defaultValues obj IS optional
but RHF doesn't mess with
useStates
does it? in my case partPresignedUrls
is just useState object. ie:
Might have found it, UPDATE: was not it
no it doesnt, but my point was just its kinda hard to tell whats going on without the full context
I will make a codesandbox
couldnt you just paste the whole component?
its 360 lines
thats fine
whats the type for
setPresignedUrl
in BasicDropzone
?
i think youre lying to typescript somewhere in that typedef.cause your problem is here:
the code for that is
console.log("initial src object:", partPresignedUrls[0])
so partPresignedUrls[0]
shouldnt be an arrayI am:
yes
any
theres the issue
theres a thing you can do to get the type for the setter in a setState(), but i pretty much always just make a regular setter function like
setPresignedUrl: {(newPresignedUrl: string): void}
and then pass that in
and then the logical problem in DropZone is that youre setting an array instead of an array element
does that make sense?is this about the
setPresignedUrl
inside the dropzone-component?yeah somewhere in your DropZone component you are calling the setter that was passed in thru props with an array of 1 thing instead of the individual object itself
I was struggling to with the dropzone-component here, https://ptb.discord.com/channels/966627436387266600/1088862349764591727/1088862349764591727
in general my quick opinion that nobody asked for is that you should almsot never leave the types as TODOs. the types are there to help you while youre writing the code
again, its a bit hard to help cause the context is missing. like obviously theres bits of code missing from what you pasted because the
useState()
you pasted has the name setPartPresignedUrls
and then the code below has setPresignedUrls
and for these kinds of issues the devil really is in those detailsI did keep the names different, didn't think it mattered as long as the types were the same
but then I ended up putting any
my point isnt about the names so much. the names just make it clear that there is stuff missing between the useState() you pasted and where you are using it
which might be relevant
oh yeah, I just looked at the init of the state and didn't think of the connection to the dropzone-component
oh it's not though, one index per file, and for each file we may or may not have to split it into multiple chunks, depends on file size. example: 1 large file, 1 small file
so
setPresignedUrl: {(newPresignedUrl: string): void}
doesn't work. It has to be something like: setPresignedUrl: {(newPresignedUrl: {url: string, partNumber: number}[]): void}
, but what I just wrote is not valid tsI am struggling to get the types right.
I am currently just using the types from the
useState
hook:
meaning, the props to the dropzone-component looks like this:
I can set the uploadId
with no problem:
But if I try do the same with setPresignedUrl
:
I get an ts(2345)
error message, with the full text being:
Should I post a new question titled something like "typedefs & usage of useStates with object arrays" ?
you should probably post a new question, but ill make a couple of suggestions
1) in general, keep the useState setter inside the component it came from. when your dropzone needs to make a change just have it call an onChange() function (or whatever name) with the new data. then you dont need to pass around sophisitcated types between classes.
2) the error message is saying that sometimes you are setting it with an element and sometimes you are setting it with an array
that is what the type error is about. you should make sure you are always setting an array
I don't see how I can create the onChange/updater function without getting the same type errors.
so in the same component as the component as the state was initialized in we create a onChange function:
this still returns the same error message
What I did in the dropzone-component was:
and for passing props:
creating a new question/thread so that anyone in a similar situation doesn't have to go through 50+ messages
new thread here: https://ptb.discord.com/channels/966627436387266600/1093227515524431976