chrisguttandin / dynamo-converters

A collection of converter functions to get good old JavaScript key/value objects into a DynamoDB friendly schema and back again.
MIT License
24 stars 3 forks source link

dataToItem(object) return never as type #82

Closed LasaleFamine closed 2 years ago

LasaleFamine commented 2 years ago

Hello, firstly I want to thank you for the awesome work on these converters.

I'm upgrading from v5 and I'm having a problem of types. The situation is the follow:

I have an interface like so:

interface INewUplink {
  device: number;
  providerDeviceId: string;
  fPort: number;
  fCnt: number;
  payload: Record<string, any>;
  consumedAirtime: string;
  rxMetadataRaw: Array<Record<string, unknown>>;
  settingsRaw: Record<string, unknown>;
  receivedAt: string;
}

And I'm using the object uplink as param for dataToItem function but it returns never as type, so I can't use the returned value as data for the Dynamo SDK function to create a new record:

Screenshot 2022-02-24 at 13 02 55

Instead it works if I use a simple object like:

dataToItem({ test: 'test' });

I'm thinking that can be a problem with the check here: https://github.com/chrisguttandin/dynamo-converters/blob/master/src/types/derived-item-object.ts#L4

chrisguttandin commented 2 years ago

Hi @LasaleFamine, thanks for making me aware of that regression.

I have to admit that I don't fully know what the difference is but now it seems to work. Instead of checking if something extends IDataObject the code does now separately check if the keys are valid and if the values are valid. Somehow that makes a difference to TypeScript. 🤷

But you would still need to adjust your INewUplink definition. Using unknown doesn't work. It needs to be any if you don't know the type ahead of time.

interface INewUplink {
    device: number;
    providerDeviceId: string;
    fPort: number;
    fCnt: number;
    payload: Record<string, any>;
    consumedAirtime: string;
    rxMetadataRaw: Array<Record<string, any>>;
    settingsRaw: Record<string, any>;
    receivedAt: string;
}
LasaleFamine commented 2 years ago

Hi @chrisguttandin, thanks for the quick response.

I want to add the actually I have solved the problem before your fix using a type instead of an interface for my NewUplink data structure and it worked well.

chrisguttandin commented 2 years ago

Thanks for letting me know. Although it confuses me why using a type works but using an interface doesn't. 🤷