electricessence / TypeScript.NET

A JavaScript-Friendly .NET Based TypeScript Library (Moved)
https://github.com/electricessence/TypeScript.NET-Core
Other
251 stars 36 forks source link

Offline behavior of Enumerable.select is inconsistent #59

Closed shaulbehr closed 7 years ago

shaulbehr commented 7 years ago

Here's my code:

let today = DateTime.today;
let vacationDates = Enumerable.from(vacations)
    .select(v => {
        return {
            utcStartDate: v.startDate.toUniversalTime, 
            utcEndDate: v.endDate.toUniversalTime, 
            localStartDate: v.startDate, 
            todayUtc: today.toUniversalTime
        };
    });
let first = vacationDates.first();

In normal online mode, this behaves as expected. But in offline mode, first.utcStartDate and first.utcEndDate have undefined value. first.localStartDate and first.todayUtc have the correct values. (Vacation.startDate and Vacation.endDate are DateTime values.)

electricessence commented 7 years ago

I apologize i didn't see this until now. Looking into it.

electricessence commented 7 years ago

First off. 'toUniversalTime()' is a method that you are referencing like a delegate with no proper this reference.

Try this instead:

let today = DateTime.today;
let vacationDates = Enumerable.from(vacations)
    .select(v => {
        return {
            utcStartDate: v.startDate.toUniversalTime(), 
            utcEndDate: v.endDate.toUniversalTime(), 
            localStartDate: v.startDate, 
            todayUtc: today.toUniversalTime()
        };
    });
let first = vacationDates.first();
electricessence commented 7 years ago

Lemme know if this is solved.

shaulbehr commented 7 years ago

Hi, I was taken off this project for a while; now I'm back. I just updated to TypeScript.NET 4.9.5 and tried what you suggested, i.e. adding () to .toUniversalTime. Doesn't compile:

Cannot invoke an expression whose type lacks a call signature. Type 'DateTime' has no compatible call signatures.)

electricessence commented 7 years ago

Hello again. :) As before, I can't really be sure what's wrong without your latest code sample.

shaulbehr commented 7 years ago
            let vacationDates = Enumerable.from(vacations)
                .select(v => {
                    return {startDate: v.startDate.toUniversalTime, endDate: v.endDate.toUniversalTime};
                });

where vacations is of type Vacation[]:

import {DateTime} from "typescript-dotnet-amd/System/Time/DateTime";

export class Vacation {
    public vacationId: string;
    public schoolId: string;
    public vacationDescription: string;
    public startDate: DateTime;
    public endDate: DateTime;
}
electricessence commented 7 years ago

Ok I see the issue now. This is a mistake on my part. .toUniversalTime is some how an accessor and it shouldn't be.

electricessence commented 7 years ago

But you will need to call it like this after I fix: v.startDate.toUniversalTime()

electricessence commented 7 years ago

https://github.com/electricessence/TypeScript.NET/commit/2f6ae36a058769bdf203032a53c0b25d8d33a680 So you'll need to change the use to treat it like a method. This is how the DateTimeclass in .NET is.

With the sample code you provided, I'm still uncertain why the compiler would give you grief. Please update to 4.9.6 and try the changes I suggested and let me know if it fixes it.