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?

7
  • Sorry, I corrected it now. Commented May 8, 2015 at 7: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? Commented May 8, 2015 at 7:57
  • There's no try/catch, and dob is strongly-typed as a Date. Commented May 8, 2015 at 8: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? Commented May 8, 2015 at 8:10
  • 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. Commented May 8, 2015 at 8:37

1 Answer 1

21

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;
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.