website icon indicating copy to clipboard operation
website copied to clipboard

Enable exactOptionalPropertyTypes in TypeScript

Open tsa96 opened this issue 2 years ago • 1 comments

This is a sensible flag that helps TS distinguish between fields that can be omitted and fields that can take a value of undefined. Enabling it stops you being able to set e.g. k?: string to undefined, and makes TypeScript's type inference when using index signatures a lot stronger - see https://github.com/microsoft/TypeScript/pull/43947

For us, it allows us to make nested interfaces containing optional values using extends JsonObject from type-fest, currently it just spews out errors about not being able to assign to some (e.g. string) index type, see https://github.com/sindresorhus/type-fest/issues/471#issuecomment-1284987356 for details.

TypeScript unfortunately doesn't have it on by default or in strict mode, but I'd definitely like to support it. I would add it right now, but it requires --strictNullChecks. So, once we finish https://github.com/momentum-mod/website/issues/792, this is a freebie.

I have to do a bunch of ugly as unknown as MapZones casts at the moment because of it, leaving comments everywhere that I am referencing this issue number. Once we tackle this, search for every instance and remove.

tsa96 avatar Oct 15 '23 20:10 tsa96

I have to do a bunch of ugly as unknown as MapZones casts at the moment because of it, leaving comments everywhere that I am referencing this issue number. Once we tackle this, search for every instance and remove.

I was getting this wrong -- the issue is using interfaces over types, where using types would given us implicit index signatures that satisfy Prisma's JSON types. Not 100% sure on best approach atm, tempted to convert everything to types but if we're getting rid of class-transformer DTO classes anyway then adding index signatures to every interface doesn't seem that bad. Worth thinking about this more when deciding on future approaches to DTOs and validation.

tsa96 avatar Jul 30 '25 17:07 tsa96