Git Product home page Git Product logo

Comments (3)

maximilianschmitt avatar maximilianschmitt commented on June 8, 2024 3

Thanks a lot for the tip @malcolmkiano! I no longer get type error from the original issue.

Do you have any tips to make the TypeScript types a bit easier to work with?

For example, homePage.heroLogo comes back as Asset<undefined, string> | { sys: AssetLink; } which means I have to do these kinds of maneuvers before accessing the data inside:

const heroLogo =
	'fields' in homePageEntry.heroLogo
		? {
				src: homePageEntry.heroLogo.fields.file?.url || '',
				width: homePageEntry.heroLogo.fields.file?.details.image?.width || 0,
				height: homePageEntry.heroLogo.fields.file?.details.image?.height || 0,
		  }
		: null

Is there a way to simplify this?

I'm getting the homePageEntry like this:

const homePageResult = await contentfulClient.getEntries<TypeHomePageSkeleton>({ content_type: 'homePage', include: 2 })
const homePageEntry = homePageResult.items?.[0]?.fields || null

These are the types generated by the CLI:

import type { ChainModifiers, Entry, EntryFieldTypes, EntrySkeletonType, LocaleCode } from "contentful";

export interface TypeHomePageFields {
    heroLogo: EntryFieldTypes.AssetLink;
    heroTitle: EntryFieldTypes.Symbol;
    heroText: EntryFieldTypes.Text;
    descriptionText: EntryFieldTypes.RichText;
    descriptionImage?: EntryFieldTypes.AssetLink;
}

export type TypeHomePageSkeleton = EntrySkeletonType<TypeHomePageFields, "homePage">;
export type TypeHomePage<Modifiers extends ChainModifiers, Locales extends LocaleCode> = Entry<TypeHomePageSkeleton, Modifiers, Locales>;

from cf-content-types-generator.

malcolmkiano avatar malcolmkiano commented on June 8, 2024 1

You should provide the -X flag when running the command to generate types that satisfy the V10 requirements. This will probably end up being the default behavior at some point in the future:

cf-content-types-generator --spaceId $CONTENTFUL_SPACE_ID --token $CONTENTFUL_MANAGEMENT_TOKEN -o src/contentful-types -X

from cf-content-types-generator.

marcolink avatar marcolink commented on June 8, 2024 1

@malcolmkiano thanks for resolving this 🎉

from cf-content-types-generator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.