Skip to content

Commit

Permalink
feat(codegen): expose referenced type as hidden symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
sgulseth committed Mar 15, 2024
1 parent 015f4f6 commit 6b34289
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
23 changes: 23 additions & 0 deletions packages/@sanity/codegen/src/typescript/typeGenerator.ts
Expand Up @@ -11,6 +11,8 @@ import {
type UnionTypeNode,
} from 'groq-js'

const REFERENCE_SYMBOL_NAME = 'internalGroqTypeReferenceTo'

/**
* A class used to generate TypeScript types from a given schema
* @internal
Expand Down Expand Up @@ -71,6 +73,18 @@ export class TypeGenerator {
return new CodeGenerator(t.exportNamedDeclaration(typeAlias)).generate().code
}

static generateKnownTypes(): string {
const typeOperator = t.tsTypeOperator(t.tsSymbolKeyword())
typeOperator.operator = 'unique'

const identifier = t.identifier(REFERENCE_SYMBOL_NAME)
identifier.typeAnnotation = t.tsTypeAnnotation(typeOperator)

const decleration = t.variableDeclaration('const', [t.variableDeclarator(identifier)])
decleration.declare = true
return new CodeGenerator(t.exportNamedDeclaration(decleration)).generate().code
}

/**
* Since we are sanitizing identifiers we migt end up with collisions. Ie there might be a type mux.video and muxVideo, both these
* types would be sanityized into MuxVideo. To avoid this we keep track of the generated type names and add a index to the name.
Expand Down Expand Up @@ -204,6 +218,15 @@ export class TypeGenerator {
}
}
}
if (typeNode.dereferencesTo !== undefined) {
const derefType = t.tsPropertySignature(
t.identifier(REFERENCE_SYMBOL_NAME),
t.tsTypeAnnotation(t.tsLiteralType(t.stringLiteral(typeNode.dereferencesTo))),
)
derefType.computed = true
derefType.optional = true
props.push(derefType)
}
return t.tsTypeLiteral(props)
}

Expand Down
Expand Up @@ -58,7 +58,10 @@ async function main() {
const schema = await readSchema(opts.schemaPath)

const typeGenerator = new TypeGenerator(schema)
const schemaTypes = typeGenerator.generateSchemaTypes()
const schemaTypes = [
typeGenerator.generateSchemaTypes(),
TypeGenerator.generateKnownTypes(),
].join('\n')
const resolver = getResolver()

parentPort?.postMessage({
Expand Down

0 comments on commit 6b34289

Please sign in to comment.