Declaring object of arrays in Typescript; getting error Type '{}' missing the following properties

gcr

I have the following code:

  const qn = props.question.question // == question number
  const statusLib: { [index: number]: {} } = {
    82: ["70%", "0%", "0%", 1, 1],
    83: ["70%", "0%", "0%", 1, 2],
    84: ["100%", "45%", "0%", 2, 3],
    85: ["100%", "70%", "0%", 2, 3],
    86: ["100%", "100%", "46%", 3, 3],
  }
  const s:Array<ReactText> = statusLib[qn] // squiggly under 's'. Should return array

I want to keep it short and sweet. This should be min reproducible example. I tried various permutations, with about the same result.

I never had a problem rendering on local server. It's strictly a typescript enforcement thing.

Type '{}' is missing the following properties from type 'any[]': length, pop, push, concat, and 28 more.ts(2740)

I'm still learning the semantics of TS but my understanding is from this const statusLib: { [index: number]: {} } = {

you're declaring how the the object should be indexed, and then that it is an object of the most general type.

My assignment s = ... obviously returns an array. If I wasn't my code wouldn't work at all. Typescript thinks it's an object, so maybe I'm accidentally telling it that. I tried various ways to tell it all the contents are arrays such as :

const statusLib: { [index: number]: {[]} } = { and s:any[]

StatusLib is an object indeed but it's children are all arrays. How do I tell TS this?

k0pernikus

You have an array like ["70%", "0%", "0%", 1, 1] that you typehint as an empty object {}. You at least need an array [], which could be any[] (worst), unknown[] (still bad), (string|number)[] (getting close), yet since your array is consistent it seems to be a tuple that you could type in the form of:

type MyTuple = [string, string, string, number, number];

Then your typing should become

 const statusLib: { [index: number]: MyTuple }

(You don't need the custom type defintion. It may be useful in the long run.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Declaring object of arrays in Typescript; getting error Type '{}' missing the following properties

From Dev

TypeScript React Functional Component - is missing the following properties from type 'Element': type, props, key error

From Dev

In R, getting the following error: "attempt to replicate an object of type 'closure'"

From Dev

Typescript conditional type missing properties

From Java

Typescript: Type X is missing the following properties from type Y length, pop, push, concat, and 26 more. [2740]

From Dev

Not getting an error for following implementation of Interface in Typescript

From Dev

Declared properties won't be serialized if its declaring type is a dynamic object

From Dev

Declaring the type of 'this' in a typescript function?

From Dev

What can I replace 'any' with while declaring an object type in Typescript?

From Dev

Getting "TypeError: 'type' object is not subscriptable" with arrays

From Dev

Typescript Error: Property 'mapType' is missing in type '{}'

From Dev

TypeScript object implementing interface with extra properties error

From Dev

Declaring array in typescript and typeof is an object

From Dev

getting object properties and methods from multidimensional arrays in Javascript

From Dev

I am getting error for following code and warning that The type ClipboardManager is deprecated

From Dev

Why am I getting an uncaught type error with the following code?

From Dev

Getting the following error for datetime

From Dev

Typescript error: Property 'children' is missing in type but required in type 'CommonProps'

From Dev

Declaring class type variable from other class gives C2143 error (missing ';' before '*" )

From Dev

Java- Getting unexpected type error when declaring new generic set

From Dev

Access VBA: Error when declaring the type of a Database object: User defined type is not defined

From Dev

Typescript: If am defining a variable type as interface type am getting error

From Dev

Declaring multiple TypeScript variables with the same type

From Dev

TypeScript - get the declaring type from a static method

From Dev

Declaring string type with min/max length in typescript

From Dev

Getting properties object in spring

From Dev

getting Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays

From Dev

Getting a "No instance of Applicative" error when declaring a Monad

From Dev

Copy a object properties to another in TypeScript, cause error TS2339

Related Related

  1. 1

    Declaring object of arrays in Typescript; getting error Type '{}' missing the following properties

  2. 2

    TypeScript React Functional Component - is missing the following properties from type 'Element': type, props, key error

  3. 3

    In R, getting the following error: "attempt to replicate an object of type 'closure'"

  4. 4

    Typescript conditional type missing properties

  5. 5

    Typescript: Type X is missing the following properties from type Y length, pop, push, concat, and 26 more. [2740]

  6. 6

    Not getting an error for following implementation of Interface in Typescript

  7. 7

    Declared properties won't be serialized if its declaring type is a dynamic object

  8. 8

    Declaring the type of 'this' in a typescript function?

  9. 9

    What can I replace 'any' with while declaring an object type in Typescript?

  10. 10

    Getting "TypeError: 'type' object is not subscriptable" with arrays

  11. 11

    Typescript Error: Property 'mapType' is missing in type '{}'

  12. 12

    TypeScript object implementing interface with extra properties error

  13. 13

    Declaring array in typescript and typeof is an object

  14. 14

    getting object properties and methods from multidimensional arrays in Javascript

  15. 15

    I am getting error for following code and warning that The type ClipboardManager is deprecated

  16. 16

    Why am I getting an uncaught type error with the following code?

  17. 17

    Getting the following error for datetime

  18. 18

    Typescript error: Property 'children' is missing in type but required in type 'CommonProps'

  19. 19

    Declaring class type variable from other class gives C2143 error (missing ';' before '*" )

  20. 20

    Java- Getting unexpected type error when declaring new generic set

  21. 21

    Access VBA: Error when declaring the type of a Database object: User defined type is not defined

  22. 22

    Typescript: If am defining a variable type as interface type am getting error

  23. 23

    Declaring multiple TypeScript variables with the same type

  24. 24

    TypeScript - get the declaring type from a static method

  25. 25

    Declaring string type with min/max length in typescript

  26. 26

    Getting properties object in spring

  27. 27

    getting Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays

  28. 28

    Getting a "No instance of Applicative" error when declaring a Monad

  29. 29

    Copy a object properties to another in TypeScript, cause error TS2339

HotTag

Archive