diff --git a/src/cf-definitions-builder.ts b/src/cf-definitions-builder.ts index 0a2c388..4cf5303 100644 --- a/src/cf-definitions-builder.ts +++ b/src/cf-definitions-builder.ts @@ -199,18 +199,26 @@ export default class CFDefinitionsBuilder { declaration: InterfaceDeclaration, field: Field, ): void => { + const type = renderProp(field); declaration.addProperty({ name: field.id, hasQuestionToken: field.omitted || (!field.required), - type: renderProp(field), + type, }); - // eslint-disable-next-line no-warning-comments - // TODO: dynamically define imports based on usage - file.addImportDeclaration({ - moduleSpecifier: 'contentful', - namespaceImport: 'Contentful', - }); + if (type.includes('Contentful.')) { + file.addImportDeclaration({ + moduleSpecifier: 'contentful', + namespaceImport: 'Contentful', + }); + } + + if (type.includes('EntryLink')) { + file.addImportDeclaration({ + moduleSpecifier: '@src/types/contentful/static', + namedImports: ['EntryLink'], + }); + } file.addImportDeclaration({ moduleSpecifier: '@contentful/rich-text-types', diff --git a/src/renderer/cf-render-prop-link.ts b/src/renderer/cf-render-prop-link.ts index a9202ac..01a2467 100644 --- a/src/renderer/cf-render-prop-link.ts +++ b/src/renderer/cf-render-prop-link.ts @@ -4,7 +4,7 @@ import {renderUnionType} from './render-union-type'; const linkContentType = (field: Pick): string => { const validations = linkContentTypeValidations(field); - return validations?.length > 0 ? renderUnionType(validations.map(moduleName)) : 'CMSEntries'; + return validations?.length > 0 ? renderUnionType(validations.map(name => `EntryLink<${moduleName(name)}>`)) : 'EntryLink'; }; export const renderPropLink = (field: Pick) => {