Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript: Better Generic Support for td.object() #236

Closed
sgtoj opened this issue May 3, 2017 · 8 comments
Closed

TypeScript: Better Generic Support for td.object() #236

sgtoj opened this issue May 3, 2017 · 8 comments

Comments

@sgtoj
Copy link
Contributor

sgtoj commented May 3, 2017

Given the following...

import * as td from "testdouble";

class FooBar {
  private _date: Date;
  constructor() {
    this._date = new Date();
  }
  public dateString() {
    return this._date.toString();
  }
}

let fooBarFake = td.object<FooBar>("FooBar" as any);

The above code will set fooBarFake to FooBar object as expected. TypeScript and fooBarFake behaves as I expect too. However, without as any, TypeScript will throw the following error...

'"FooBar"' is not assignable to parameter of 'FooBar'

Take notice of the quote marks. Without the generic parameter (<FooBar> ), TypeScript fooBarFake thinks it of String type.

I believe the parameter should not need to be cast as any type for the generic parameter to be accepted.

import * as td from "testdouble";

// define FooBar class here

let fooBarFake = td.object<FooBar>("FooBar");

As side note, I generally set TypeScript's noImplicitAny setting is to true.

@searls
Copy link
Member

searls commented May 3, 2017

I have no idea how to TypeScript. Would you send a PR?

@sgtoj
Copy link
Contributor Author

sgtoj commented May 3, 2017

I will look in to the source later (tonight afterwork?). I don't think the solution will be too difficult. Believe there only needs to be a small change to the way Proxy objects are created.

@searls
Copy link
Member

searls commented May 3, 2017

Thanks! I met a few very nice people at NG Conf who offered to help make the typescript story for testdouble really shine, so we can find more help if needed

@searls
Copy link
Member

searls commented May 3, 2017

Related, when you dig in there is a test:typescript script but I can't tell that it actually does anything. Any fix to that (or a new test that actually fails when broken) would be awesome

@sgtoj
Copy link
Contributor Author

sgtoj commented May 4, 2017

I have determined the caused TypeScript error is due to lodash's .tap() function -- docs. More specifically, I believe it's due to the function's interface definition defined in @types/lodash. Furthermore, it is working as designed: the explicit generic type parameter is suppose to match the first parameter's type.

I believe solution is creating a TypeScript definition for testdouble.js. Probably what the guys at the NG Conf were thinking too.

@searls
Copy link
Member

searls commented May 4, 2017 via email

@sgtoj
Copy link
Contributor Author

sgtoj commented May 4, 2017

You're right! Thanks! Overloading the object() function works too. I will push a PR soon.

@sgtoj
Copy link
Contributor Author

sgtoj commented May 4, 2017

Pull #237 submitted. As for your question about test:typescript, it does what it can because there is no real way to test *.d.ts. So that npm script runs the TS compiler against a test.ts script without generating a compiled script. If there were any type errors, the tsc throw them.

@searls searls closed this as completed in a704660 May 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants