-
Notifications
You must be signed in to change notification settings - Fork 54
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
Fields are incorrectly extending Entry #108
Comments
I'm having a similar issue. where
this is what is generated for one of my components:
and this is what passes the type checks:
I also need to add I tried downgrading to a previous version of the Contentful repo v10 beta to see if that fixed it but it did not @GabrielAnca are you guys still using it with this issue? seems like it would break something down the line |
Hi 👋🏻 Thanks a lot for sharing this with us, and sorry for the late response Starting from the end, we're still using this repo on our day-to-day indeed. @asimpson good point 🤔 We never had this issue because we don't use @intcreator we are hardcoding the sys interface and we actually shouldn't do that. We only intend to hardcode the content type to help typescript with typing. Your proposed solution makes perfect sense to me. An alternative I was thinking about was actually removing duplication by just extending Contentful's export interface ICtaLink extends Entry<ICtaLinkFields> {
sys: Sys & {
contentType: {
sys: {
id: 'ctaLink'
linkType: 'ContentType'
type: 'Link'
}
}
}
} Would something like this work? |
@GabrielAnca I believe that would work. if it passes the type check immediately after generation that's pretty much all I need |
I've been experiencing, what I think is a related issue – I'm using version After I generate the types, I get something like this: export interface ICategory extends Entry<ICategoryFields> {
sys: {
id: string;
type: string;
createdAt: string;
updatedAt: string;
locale: string;
contentType: {
sys: {
id: "category";
linkType: "ContentType";
type: "Link";
};
};
};
} I then create a function to fetch this entry, that looks like this: export const fetchCategories = async (): Promise<
EntryCollection<ICategoryFields>
> => {
return await Contentful.getEntries<ICategory["fields"]>({
content_type: "category",
});
}; The issue here is that the type for So, when I try to feed this data into a component like so: const categories = await fetchCategories();
type IHomeProps = {
categories: ICategory[];
}
<Home categories={categories.items} /> TS gives me the following error: Type 'Entry<ICategoryFields>[]' is not assignable to type 'ICategory[]'.
Type 'Entry<ICategoryFields>' is not assignable to type 'ICategory'.
The types of 'sys.contentType.sys.id' are incompatible between these types.
Type 'string' is not assignable to type '"category"'.ts(2322) I've tried a number of different things, but can't get it to work. Short of casting (which I would rather avoid as it defeats the purpose of TS), does anyone know how to get around this? |
I've also exhausted all options trying to get this to work in userland. Anybody else had any luck? |
I am not 100% sure if I did this right but I got the typing mostly working by:
It fixes the TS, but no idea if these types will actually be returned by API calls. Before: // type error, this is from raw generated types
export interface IPage extends Entry<IPageFields> {
sys: {
id: string;
type: "Entry";
createdAt: string;
updatedAt: string;
locale: string;
contentType: {
sys: {
id: "page";
linkType: "ContentType";
type: "Link";
};
};
}
} After: // grab the EntitySys from contentful
import { EntitySys } from "contentful";
// create a utility type
interface WithFieldsType<Fields> {
contentTypeId: string;
fields: Fields;
}
// use the utility type instead on the original fields
export interface IPage extends Entry<WithFieldsType<IPageFields>> {
sys: {
id: string;
type: "Entry";
createdAt: string;
updatedAt: string;
locale: string;
contentType: {
sys: {
id: "page";
linkType: "ContentType";
type: "Link";
};
};
} & EntitySys; // extend sys here
} Types working now. |
Okay, I did one more patch because apparently the import {
type Asset,
type Entry, // this sucks
type EntrySys,
type BaseEntry,
} from "contentful";
export interface IPage extends WithFieldsType<IPageFields>, BaseEntry {
sys: {
id: string;
type: "Entry";
createdAt: string;
updatedAt: string;
locale: string;
contentType: {
sys: {
id: "page";
linkType: "ContentType";
type: "Link";
};
};
} & EntrySys;
} |
My generated type looks something like this:
LinkFields
looks something like this:and for reference,
Entry
's type signature is this:Entry
includestoPlainObject
andupdate
neither of which are present on fields of anEntry
(to the best of my understanding).My understanding of the "resolved" type signature looks like this:
This is incorrect, it should be something like this:
The text was updated successfully, but these errors were encountered: