Skip to content

Generic type is being ignored #26639

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

Closed
pleerock opened this issue Aug 23, 2018 · 6 comments
Closed

Generic type is being ignored #26639

pleerock opened this issue Aug 23, 2018 · 6 comments
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@pleerock
Copy link

export interface Category {
    id: number;
    name: string;
    posts?: Post[];
}

export interface Post {
    id: number;
    title: string;
    category: Category;
}

export interface Photo {
    id: number;
    filename: string;
}

export class Model<ModelType> {
    constructor(name: string) {
    }
}

export const PostModel = new Model<Post>("Post");
export const CategoryModel = new Model<Category>("Category");
export const PhotoModel = new Model<Photo>("Photo");

export type ResolverOptions<ModelType> = {
    [P in keyof ModelType]?: { type: Model<ModelType[P]> }
}

const options: ResolverOptions<Post> = {
    category: {
        type: PostModel
    }
}

Last line (type: PostModel) should give an error.

It does know what type to expect right:

screenshot 2018-08-23 23 29 04

However it accepts any instance of Model - PostModel, CategoryModel, PhotoModel without any error. Why?

screenshot 2018-08-23 23 30 38

p.s. sorry for the bad title, hope somebody can help to name this issue properly

@ghost
Copy link

ghost commented Aug 23, 2018

In Model<ModelType>, ModelType is unused. So Model<number> is assignable to Model<string> because there's no place for them to conflict. A quick fix is to add a declaration like __nominal!: ModelType; to Model.

@ghost ghost added the Question An issue which isn't directly actionable in code label Aug 23, 2018
@pleerock
Copy link
Author

wow this is so non obvious, thanks for the answer

@pleerock
Copy link
Author

Don't want to spam issues, but is it treated as bug?

I have following error:

screenshot 2018-08-23 23 54 20

But it does not provide a suggestion

screenshot 2018-08-23 23 55 09

@ghost
Copy link

ghost commented Aug 23, 2018

@pleerock Could you post a small complete example as plaintext that I could use to verify?

@pleerock
Copy link
Author

@Andy-MS there you are:

export class Model<ModelType> {
    __nominal!: ModelType;
    constructor(name: string) {
    }
}

export interface Category {
    id: number;
    name: string;
    posts?: Post[];
}

export interface Post {
    id: number;
    title: string;
    categories: Category;
}

export const PostModel = new Model<Post>("Post");
export const CategoryModel = new Model<Category>("Category");

export type ResolverOptions<ModelType> = {
    [P in keyof ModelType]?: { type: Model<ModelType[P]> }
}

export function resolver<ModelType>(
    model: Model<ModelType>,
    resolver: ResolverOptions<ModelType>
) {
}
export const PostResolver = resolver(PostModel, {
    categories: {
        // test autocompletion here
    }
});

@ghost ghost added Bug A bug in TypeScript Fixed A PR has been merged for this issue and removed Question An issue which isn't directly actionable in code labels Aug 23, 2018
@ghost
Copy link

ghost commented Aug 23, 2018

Ah, that's because resolver doesn't get its type argument inferred. Should be fixed by #26646.

@ghost ghost closed this as completed in #26646 Aug 27, 2018
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

1 participant