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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not infer return type... #12

Closed
renehauck opened this issue Apr 15, 2018 · 4 comments
Closed

Could not infer return type... #12

renehauck opened this issue Apr 15, 2018 · 4 comments

Comments

@renehauck
Copy link

Hi,

first, I will thank you for this awesome package. 馃憤
Currently we are start a new project and would like to implement this package (and give you feedback, if you like).
I think I found the first bug 馃槈.
My code:

import { Schema, Query, ObjectType, Field, Mutation, compileSchema } from 'typegql';

@ObjectType({ description: 'Simple product object type' })
class Product {
    @Field()
    isExpensive() {
        return this.price > 50;
    }
    @Field() name: string;

    @Field() price: number;

}

@Schema()
class SuperSchema {
    @Query()
    hello(): Product {
        return new Product();
    }
}

export const compiledSchema = compileSchema(SuperSchema);

Errostack:

        return _super !== null && _super.apply(this, arguments) || this;
                                         ^
Error: @ObjectType Product.isExpensive: Could not infer return type and no type is forced. In case of circular dependencies make sure to force types of instead of infering them.
source-map-support.js:439
    at FieldError.BaseError [as constructor] (/home/rene/Projects/jennyjs/node_modules/typegql/lib/index.js:280:42)
    at new FieldError (/home/rene/Projects/jennyjs/node_modules/typegql/lib/index.js:710:24)
    at inferTypeOrThrow (/home/rene/Projects/jennyjs/node_modules/typegql/lib/index.js:421:15)
    at resolveRegisteredOrInferedType (/home/rene/Projects/jennyjs/node_modules/typegql/lib/index.js:641:12)
    at compileFieldConfig$$1 (/home/rene/Projects/jennyjs/node_modules/typegql/lib/index.js:659:24)
    at /home/rene/Projects/jennyjs/node_modules/typegql/lib/index.js:692:39
    at Array.forEach (<anonymous>)
    at getAllFields (/home/rene/Projects/jennyjs/node_modules/typegql/lib/index.js:687:25)
    at /home/rene/Projects/jennyjs/node_modules/typegql/lib/index.js:700:39
    at Array.forEach (<anonymous>)

Is this a Bug, or what I'm doing wrong?
Thanks

@MichalLytek
Copy link

this is behaving as intended. we only serialize explicit types. implicit types are not serialized. this is the only way to make scenarios like single-file transpile works.

microsoft/TypeScript#18995

@renehauck
Copy link
Author

very fast...thank you

@pie6k
Copy link
Collaborator

pie6k commented Apr 15, 2018

Right, you need to be explicit about return type of function. In your case you'd need to tell it returns boolean even if it's obvious in function code.

    isExpensive(): boolean { // <-- `boolean` return type is explicit here and that's needed for typegql to know it
        return this.price > 50;
    }

@cgatian
Copy link

cgatian commented Jun 7, 2018

Doesnt the documentation show using implicit types?

  @Field()
  isExpensive() { // <--- Missing boolean
    return this.price > 50;
  }

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

4 participants