0

I have declared my data structures and trying to get the data from the backend. The data which were coming from backend were JSON object and it is not implicitly converting the data from string to any other type which I was defined.

        For Ex :

        export interface Model { 
             data1 : EnumType,
             data2 : date
        } 

        export enum EnumType{
          Up,
          Down,
          Left,
          Right,
        }

    export class Data{
        public getData() {

            const url: string = '/data';
            let resultData : Model[] = this.http.get<Model[]>(url);
            console.log('result Data...' , resultData)
        }
    }


We are getting Data from backend like this : data1 : "UP" , data2 : '2012-01-26T13:51:50.417-07:00'


**Output:**
result Data... data1 : "UP" , data2 : '2012-01-26T13:51:50.417-07:00'

**Expected Output:**
result Data... data1 : 1(enum type) , data2 : 2012-01-26T13:51:50.417-07:00(Date format)

(OR) it should throw error since the data we are sending doesnt match.

Why it is not implicitly converting if we are using "noImplicitAny" in tsConfig.json whether it will convert implicitly?

Nasrul Bharathi
  • 163
  • 2
  • 10
  • 1
    Why *would* it? TypeScript is purely a compile time tool; it does not exist at runtime, so cannot do any validation, casting or conversion. – jonrsharpe May 09 '18 at 07:27
  • Possible duplicate of [Angular HttpClient is not returning quite the objects I expect](https://stackoverflow.com/questions/50165067/angular-httpclient-is-not-returning-quite-the-objects-i-expect) – jonrsharpe May 09 '18 at 07:29
  • Or maybe https://stackoverflow.com/q/46022440/3001761, https://stackoverflow.com/q/48102824/3001761, ... – jonrsharpe May 09 '18 at 07:31
  • Create the explicit constructor with typed paramaters for your Model and use it. Typescript can generate checks/conversions for function parameters. – Thinkeye May 09 '18 at 07:42

3 Answers3

1

Your expectation comes from a misunderstanding what TypeScript actually is. TS is not designed change any JavaScript behavior. There are two main extensions to JS by TS: a type layer and the ability to transpile to a lower version of JS (for example using classes for an ES5 output).

You should see the type layer as (very powerful) annotations to your code. It describes what type you expect an object to be, it does not specify the type. One of the most common runtime errors I encounter when working with TS is that an object does not have the type that I have declared. So all runtime type conversions still have to be done by you. In this case it would be a call to JSON.parse(). Another alternative is to use JQuery, where $.ajax or $.get does all the heavy lifting for you, including a JSON conversion, if you specify a JSON return type.

The noImplicitAny option refers to something else. When you have this option turned on, you must add type annotations to your code, even if you want the object to be of type any. With the option turned off, a variable without annotation is implicitly typed any. Again, this is purely declarative, it does not change any behavior.

Sefe
  • 13,731
  • 5
  • 42
  • 55
-1

TypeScript does its type checking during compile-time only. It does interfere with the runtime type, because it's just regular JavaScript when compiled (transpiles to JavaScript). Getting something from the server is happening at runtime, so it has "nothing" to do with the type system of TypeScript.

Nonetheless, it might help to enrich your enum type:

enum Direction {
   Up = "UP",
    Down = "DOWN",
    Left = "LEFT",
    Right = "RIGHT",

}

Lorenz Merdian
  • 722
  • 3
  • 14
  • Is there any other option which will do implicitly in runtime ? – Nasrul Bharathi May 09 '18 at 07:31
  • I do not know a good way to do this implicitly during runtime. Only `Direction` as enum (with the values provided above) can be interpreted as a `Direction` instead of a simple string. You would have to do the conversion from `string` to `Date` explicitly. – Lorenz Merdian May 09 '18 at 08:20
-1

You should set:

export const enum EnumType{
          Up = 1,
          Down = 2,
          Left = 3,
          Right = 4,
        }

When you call it, you call it like EnumType.UP and you will get 1 as result. More on enums here.

Joe Belladonna
  • 1,349
  • 14
  • 20