19

I'm using TypeScript 1.4 in an ASP.NET MVC 5 project.

I have a field of type Date, and it works partially:

var dob: Date = result.dateOfBirth;
alert(dob);
var dobAsString = dob.toDateString();

In the code above, the first two lines work, showing the value as "1968-11-16T00:00:00", as expected. But the last line doesn't work, in fact the rest of the code below that line isn't even executed -- it just breaks, without error message.

This behavior persists no matter which Date function I apply in the last line; I could also use dob.getFullYear() etc. and it would fail every time. Yet the variable is of the right type and has the right value. The compiler also finds the Date functions, it compiles without a hitch but at runtime it fails. Any ideas?

Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
  • Sorry, I corrected it now. – Roy Dictus May 08 '15 at 07:49
  • That's a strange alert - when I alert a date object, i see the date in a completely different format, although that may be browser specific. Are you sure `result.dateOfBirth` is really a `Date` object and not a string representing a date? Is it possible there's a `try/catch` around this code that is swallowing the error silently? – DCoder May 08 '15 at 07:57
  • There's no `try/catch`, and `dob` is strongly-typed as a `Date`. – Roy Dictus May 08 '15 at 08:03
  • 1
    Typescript only enforces types at compile time, not at runtime. If, at runtime, your `result` through some fluke happens to have the number `42` or the string `"carrot"` as the value of `dateOfBirth`, Typescript cannot detect this and work around it. Can you `alert(typeof dob)` or `alert(Object.prototype.toString.call(dob))` to verify that it is indeed what you think it should be? – DCoder May 08 '15 at 08:10
  • You are right! It was a string! I thought that this was not possible; since `result.dateOfBirth` is strongly typed as a `Date`, how can it turn into a string? I don't get that. But anyway, you are absolutely right. Thank you! – Roy Dictus May 08 '15 at 08:25
  • I changed the top line to `var dob: Date = new Date(result.dateOfBirth.toString());` and that does the job. Thank you again! – Roy Dictus May 08 '15 at 08:28
  • 1
    Typescript's type specifications are just *compiler hints* - the compiler verifies that such operations are possible on such types, but it does not create any constructors/conversion code to enforce those types at runtime. If you use Typescript to specify "this AJAX call returns an object whose `dateOfBirth` property is a `Date`", the compiler will believe you, and there won't be any runtime code to verify this. – DCoder May 08 '15 at 08:37

1 Answers1

22

There are two aspects to this one. The first is that you need to parse the date, as you have a string representation currently. The second is that your result variable doesn't have type information.

var result = {
    dateOfBirth: '1968-11-16T00:00:00'
};

// Error, cannot convert string to date
var a: Date = result.dateOfBirth;

// Okay
var b: Date = new Date(result.dateOfBirth);

var result2: any = result;

// Okay (not type information for result2)
var c: Date = result2.dateOfBirth;

When you get back a JSON message, you can apply an interface to it that describes what the server has send, in order to catch problems in your TypeScript code - such as the one you found. This will stop the problem occurring again in the future (although doesn't check the supplied JSON matches the interface)... the example below assumes result currently has the any type.

interface NameYourResult {
    dateOfBirth: string;
}

var r: NameYourResult = result;
Fenton
  • 241,084
  • 71
  • 387
  • 401
  • You would be using the interface to describe the JSON message, so yes - you could have an error in the interface definition, but *only* there (and not littered throughout your code base). You would be using the interface to allow TypeScript to ensure the code *using* the message didn't make a mistake - not to validate the response from the server. i.e. you get 50% of the type safety using the interface, 0% if you don't use the interface. – Fenton May 08 '15 at 14:54