Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

How to define TypeScript Type for Object with both static and dynamic key? [duplicate]

$
0
0

I have a normalized redux structure addressesState as below. I need to define the type of that as specified in the comments

interface iAddressItem {    firstName: string;    lastName: string;    street: string;}const addressesState = {    // key "ids" is static    // value is Array<string>    ids: ["1345", "1346"],    // typeof key is string and it is dynamic    // typeof value is iAddressItem"1345": {        firstName: "Cinta",        lastName: "Riardo",        street: "Cake Street 34",    },    // typeof key is string and it is dynamic    // typeof value is iAddressItem"1346": {        firstName: "Nosipho",        lastName: "Tracey",        street: "Baker Street 42",    },};

I tried to define the type of addressesState as below:

type AddressesType = {    [key in string | number]?: iAddressItem;} & {    ids?: Array<number>;};

But when I'm initializing the redux state as:

const initalAddressesState: AddressesType = {    ids: []}

I'm getting the error:

Type '{ ids: never[]; }' is not assignable to type 'AddressesType'.  Type '{ ids: never[]; }' is not assignable to type '{ [x: string]: iAddressItem | undefined; [x: number]: iAddressItem | undefined; }'.    Property 'ids' is incompatible with index signature.      Type 'never[]' is missing the following properties from type 'iAddressItem': firstName, lastName, street(2322)

Any bits of help or insights would be great


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>