2

I don't know what's going on here... but apparently TS is not recognizing Date as Date, instead it thinks it is a string.

Here's the code I use to save the Date:

var validUntil = new Date();

validUntil.setSeconds(validUntil.getSeconds() + tokenResponse.expires_in);
tokenResponse.valid_until = validUntil;

And this is my TokenResponse class:

export class TokenResponse {
    access_token: string;
    token_type: string;
    expires_in: number;
    valid_until: Date;
    error: string;
    error_description: string;
}

And here is where the exception is thrown:

userToken && userToken.valid_until.getTime()  >= new Date().getTime()

Here's the exception:

enter image description here

Any ideas on what's wrong?

Thanks!

EDIT

By the way, I don't think it's related to Date variable works, but functions on it do not since I'm creating a new Date

7
  • Post a complete minimal example reproducing the problem. We have no idea what userToken is, among other things. Commented Nov 11, 2016 at 16:50
  • @JBNizet it comes from my API. userToken == TokenResponse Commented Nov 11, 2016 at 16:53
  • If it comes from your API, I guess it's JSON, and JSON doesn't have Dates. Only string, number, boolean. And a JSON object will never be an instance of any class. Again, a complete example would help. Commented Nov 11, 2016 at 16:54
  • @JBNizet I thought the information there would be enough. Is there anything specific that would help? I receive the response from the server (1st piece of code), my class is TokenResponse and when I try to use it it's not loading as Date (apparently) Commented Nov 11, 2016 at 16:55
  • @JBNizet you are correct about JS, though I'm creating a new instance of Date. I'm not relying on the response. Commented Nov 11, 2016 at 16:57

1 Answer 1

2

In code that isn't posted here, the value of userToken.validUntil is being set as a string. You will see that if you check typeof userToken.validUntil before the part of the code where calling getTime() errors.

Make sure when setting userToken.validUntil that you always assign a Date object to it. If you're deserializing JSON to userToken, then make sure the deserializer converts date strings to date objects or for a quick fix do (though not recommended):

userToken.validUntil = new Date(userToken.validUntil as any);

Note the following:

var o = { d: new Date() };
localStorage.setItem("test", JSON.stringify(o));
o = JSON.parse(localStorage.getItem("test"));
typeof o.d === "string"; // true
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks David, the only time I set validUntil is there :/ The only thing I can think of is maybe the local storage is changing the type?
I just checked your update. Isn't that the same as what I was doing? var validUntil = new Date(); validUntil.setSeconds(validUntil.getSeconds() + tokenResponse.expires_in); tokenResponse.valid_until = validUntil;
Ok... so the problem is related to the storage... is there a way to force it to save Date?

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.