Undefined TypeScript error

kind of a nooby question but my fool proof solution of adding the null operator when ever this happens has failed me. I appreciate the help :3
No description
T
TrialEnjoyer43d ago
Add a fallback or add an if statement to make sure its defined. Or if you feel its too tedious, theres always //@ts-expect-error So like add const Fallback = (whatever number); then at the end of skillRating statement, add ?? Fallback - or something alike. Would that work for you?
N
NeoFox43d ago
Hmm it still doesnt work
No description
N
NeoFox43d ago
But i can just stick with //@ts-expect-error for now
J
Jay43d ago
It seems to be happening because skillRating is the one that can be undefined not just the formdata, you could add an assert if you'd be sure that the index would exist every time. by doing let skillRagin = formData?.skillRating![index] notice the exclamation before accessing indexed value. For more context here's a link to ts playground: https://www.typescriptlang.org/play?#code/MYewdgzgLgBAZiATgWwCIEMroFwwN4QDWAlgDakBKmxYA5gPzZgCuyARgKaIDaAugL4wAvPiJlK1Otj78AULNIdYNACYcAHsJgAGeYtiJJtCFoQoMWAHRjyVKDVoBCbqo28YAeg8wAKgAsOGHQICC5YEAAHLkwkGGR0Qg4TQ3s6EygQGE4YEDgYKABPKJgWdi5ZIA. Lmk if this helps.
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
B
bythewayitsjosh43d ago
You can also provide another optional chaining operator to the array access:
const fallback = 1
let skillRating = formData?.skillRating?.[index] ??
fallback
const fallback = 1
let skillRating = formData?.skillRating?.[index] ??
fallback
In general I prefer using optional chaining and fallbacks rather than assuming / asserting that something will be defined if Typescript can't tell you for sure it will be. But I think either is fine. 🙂
N
NeoFox36d ago
It worked thanks! I got to figure out all these typescript quirks lol
Want results from more Discord servers?
Add your server
More Posts
UseInfiniteQuery not showing in queryHi, im using trpc from theo template and want to create infinite scrolling but useInfiniteQuery is nAuth.js (v5) with Docker?Did anyone successfully implement the latest Auth.js version in production with Docker? I have triePlanetScale Hobby not Available. Alternative Suggestions?I haven’t dealt with a database before (except Firebase’s). Is Railway a good free alternative? AlsoFree Cloud Database for prisma ? (except planetscale)Can you suggest me any other free cloud database instance other than planetscale for prisma pleaseUnique key constraint errorI am using the latest create-t3-app and I have a field that is unique in my shema. Now I want to hanwhat is Prisma.BatchPayload?```ts const insertIntoTradesRes = await ctx.db.trade.createMany({ data: trades }); const insertedTrPrevent parallel route from being triggered if hard-navigatedhttps://codesandbox.io/p/devbox/login-register-parallel-example-zw9m7m TL;DR: Try hard-navigating tInside create t3 app with Drizzle, why do we pass the database through ctx on each procedure?If I'm not mistaken, we ideally don't want to do this if we want to break our application into separbasebox in cliMy team wants to try this tool https://docs.basebox.io/getting-started/ it generates a secure backeUsage docs for DrizzleHas anyone already started writing content for https://create.t3.gg/en/usage/drizzle? If not, I'll wTRPCClientError: Unexpected token 'A', "An error o"... is not valid JSONI'm making a `fetch` API call to an external server inside a tRPC route. It works perfectly on localAccessDenied NextAuthWhat causes accessdenied?React query useInfiniteQuery (v3) and react table (v7) for server side paginationAnyone has every connected react-query v3 with react-table v7 ? Im trying to implement a server sideT3 Recommended Services Cost Scaling/PricingI've recently been exploring the T3 stack and am particularly interested in the cost-scaling aspectsprisma create code architectureHi guys I am relatively new to prisma amd I am wondering is this the correct way to use the create dHow are signals implemented?Hi all! Just saw Theo's latest on Vapor and got curious: how would one go about implementing signalsEnabling offline mode with the t3 stackI'm using the t3 stack with trpc and prisma. The application I'm building is for use on constructionHow do I add more data to session?```import type { Role } from "@prisma/client"; /** * Module augmentation for `next-auth` types. AlStripe integrationI want to implement Stripe, but I don't want to use it for subscriptions. On my website, there will Project ArchitectureHi, I am taking over a project involving building a booking an payment portal project which uses - N