Cast JSON array as type for Typescript class property

Erdna

I have made a TypeScript class like this:

export class MyClassName {
    id: number;
    name: string;
    data: /* JSON ARRAY */;
}

What I want to do is to cast the "data" property as a JSON array ( [JSON, JSON, JSON].... ).

I've tried the following:

export class MyClassName {
    id: number;
    name: string;
    data: [key: any];
}

When serving the app using "ng serve" it gives this error message but the app still functions as intended:

ERROR in src/app/myclass.ts(4,14): error TS1005: ',' expected.

but when I'm trying to build it using "ng build --prod" it fails to build due to the same error message.

Akash Dathan

If its an array of stringified JSON, you can just use string[]. Else, you can use the following.

export class MyClassName {
    id   : number;
    name : string;
    data : {[key: string]: any}[];
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

TypeScript dynamic property type on class

From Dev

class declaration - Property does not exist on type in TypeScript

From Dev

How to implicitly cast an array type to a tuple type in TypeScript?

From Java

How do I cast a JSON object to a typescript class

From Dev

Correct way to cast a JSON object into a typescript typed variable using a class?

From Dev

How to get the proper class during a cast from JSON in typescript?

From Dev

Type cast an instance of a class

From Dev

Cast a Type to a class

From Dev

Property does not exist on type 'never' on JSON array

From Dev

Public property of exported class is using private type error in TypeScript

From Dev

Typescript parent class method return type change by child property

From Dev

Angular 2 Typescript class property allows incorrect type to be assigned

From Dev

Typescript: Cast type in subscribe or in map

From Dev

Typescript: Cast type in subscribe or in map

From Dev

How do I cast a list of JSON objects into a list of TypeScript objects without losing properties on the TypeScript class?

From Dev

Typescript object property declared as array of custom type silently fails

From Dev

Typescript object property declared as array of custom type silently fails

From Dev

Property 'name' does not exist on type 'JSON' in typescript / angularJs 2

From Dev

typescript readonly property of class

From Dev

Cast to a type from a generic class

From Dev

How to deserialize to class with variable type property using NewtonSoft Json?

From Dev

Type cast array elements in for loop

From Dev

Could not cast of type Set to Array

From Dev

CollectionViewSource as Class Property Type

From Dev

generic type of class property

From Dev

class array, cast pointer in operator[]

From Dev

Typescript error : Property defined as private on type Class is defined as public on type Interface

From Dev

Typescript error : Property defined as private on type Class is defined as public on type Interface

From Dev

How to cast JSON to a DefinitelyTyped type?

Related Related

  1. 1

    TypeScript dynamic property type on class

  2. 2

    class declaration - Property does not exist on type in TypeScript

  3. 3

    How to implicitly cast an array type to a tuple type in TypeScript?

  4. 4

    How do I cast a JSON object to a typescript class

  5. 5

    Correct way to cast a JSON object into a typescript typed variable using a class?

  6. 6

    How to get the proper class during a cast from JSON in typescript?

  7. 7

    Type cast an instance of a class

  8. 8

    Cast a Type to a class

  9. 9

    Property does not exist on type 'never' on JSON array

  10. 10

    Public property of exported class is using private type error in TypeScript

  11. 11

    Typescript parent class method return type change by child property

  12. 12

    Angular 2 Typescript class property allows incorrect type to be assigned

  13. 13

    Typescript: Cast type in subscribe or in map

  14. 14

    Typescript: Cast type in subscribe or in map

  15. 15

    How do I cast a list of JSON objects into a list of TypeScript objects without losing properties on the TypeScript class?

  16. 16

    Typescript object property declared as array of custom type silently fails

  17. 17

    Typescript object property declared as array of custom type silently fails

  18. 18

    Property 'name' does not exist on type 'JSON' in typescript / angularJs 2

  19. 19

    typescript readonly property of class

  20. 20

    Cast to a type from a generic class

  21. 21

    How to deserialize to class with variable type property using NewtonSoft Json?

  22. 22

    Type cast array elements in for loop

  23. 23

    Could not cast of type Set to Array

  24. 24

    CollectionViewSource as Class Property Type

  25. 25

    generic type of class property

  26. 26

    class array, cast pointer in operator[]

  27. 27

    Typescript error : Property defined as private on type Class is defined as public on type Interface

  28. 28

    Typescript error : Property defined as private on type Class is defined as public on type Interface

  29. 29

    How to cast JSON to a DefinitelyTyped type?

HotTag

Archive