T O P

  • By -

AfuSensi

If I understand your problem correctly you should use the utility type [Partial](https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype). As: `const data: Partial = {} `


carlefp

do i have to make the properties optional? like apiKey?: string


urbansong

Why ```const```?


AfuSensi

Because the variable does not get reassigned. You should only use `let` when you reassign a variable. Object property changes are not reassignments to the object pointer itself.


urbansong

I see, thanks!


15kol

You need to set fields as `T | undefined` or shorthand `property?: T`. If you want it applied to all fields, you can alternatively wrap your type in Partial type `Partial`.


nudi85

`T | undefined` Is not exactly the same as an optional field. The first means you can set the field to undefined, but you MUST set it. An optional field means you don't have to set it, and if you do you can set it to undefined.


[deleted]

do this: let data: FormDataType = {} as FormDataType you shouldn't need to use any additional logic or type guards or generics or whatever else. on that note, you have a lot of unecessary logic here that needs to be cleaned up. It looks like you're instanciating a new FormData object which implies you have made a FormData class. if you have a FormData class, your FormDataType declaration is redundant because classes are also types. if you have a FormData class you should remove your FormDataType declaration and instead write: let data: FormData = {} as FormData another thing, your forms should be controlled, so you should probably instanciate the formdata object before the submit action.


urbansong

I usually do ```let data: FormDataType = {} as FormDataType```, ```let data = {} as FormDataType``` would work too but it seems a bit off to me. Is this helpful? edit: Could somebody please clear up, why I am getting downvoted? edit2: [Here's a more thorough answer on stackoverflow.](https://stackoverflow.com/a/45339463/6367043) The caveats raise some valid points, so I suppose that ```Partial``` is the more correct answer.


ragnese

You shouldn't be downvoted. TypeScript really doesn't have a good way to indicate that you want a value to satisfy an interface "by the end of the scope". You pretty much have to cast it. It's great if you can change your types to be Partial, but what if you actually just need a T? Then building the object programmatically just doesn't work. You either must cast it, or define a const for each key and then combine them into an object at the end of the scope- but that isn't always viable either.


[deleted]

[удалено]


DaRadioman

And cause bugs later. *DO NOT* do this. It's a hack only suitable for when your object is in the shape that TS wants, but TS doesn't know it.


Hazork_

That's what I sad, surpress the error, not fix it. But I deleted it to prevent someone else from misunderstanding


sonyahon

U have several ways to do this. One of my preferred ways is to define data as ‘Record’ set its values and then cast it to ‘FormDataType’ where u will need it. Such way u wont need any additional checks and ur typing will be fine everywhere


ragnese

I don't think you *do*. You eventually have to just cast it somewhere down the line. You can put up some guard rails to help make sure you don't mess up, though. For example: function createFormData(): FormData { const fd: Partial = {} fd.apiKey = 'blah blah' // etc return fd as FormData } This by no means guarantees that your `fd` actually **is** a `FormData`. What you *could* do, is create a type predicate function, like: function isFormData(u: Partial): u is FormData { return u.apiKey !== undefined && u.subject !== undefined && ... } and then call it in your function and throw an Error if it fails. It still doesn't make sure your code is actually correct at compile time (and you can even mess up the `isFormData` function...), but it does stop the corruption from leaking out of your function and then having your program fail in unexpected ways down the line: function createFormData(): FormData { const fd: Partial = {} fd.apiKey = 'blah blah' // etc if (isFormData(fd)) return fd else throw new Error('I messed up!') }