From 0f6661799acfb62c4d169cacc8f6867278263f0e Mon Sep 17 00:00:00 2001 From: Tim Gleeson <97414069+tagleeson@users.noreply.github.com> Date: Fri, 21 Jun 2024 00:27:41 +1000 Subject: [PATCH] fix: include type cast on ts compilation (#1962) --- .../cli/src/api/__snapshots__/compile.test.ts.snap | 2 +- packages/cli/src/api/compile.ts | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/cli/src/api/__snapshots__/compile.test.ts.snap b/packages/cli/src/api/__snapshots__/compile.test.ts.snap index c252b715b..ef777c553 100644 --- a/packages/cli/src/api/__snapshots__/compile.test.ts.snap +++ b/packages/cli/src/api/__snapshots__/compile.test.ts.snap @@ -10,7 +10,7 @@ exports[`createCompiledCatalog options.namespace should compile with global 1`] exports[`createCompiledCatalog options.namespace should compile with json 1`] = `{"messages":{"key":["Hello ",["name"]]}}`; -exports[`createCompiledCatalog options.namespace should compile with ts 1`] = `/*eslint-disable*/import type{Messages}from"@lingui/core";export const messages:Messages=JSON.parse("{\\"key\\":[\\"Hello \\",[\\"name\\"]]}");`; +exports[`createCompiledCatalog options.namespace should compile with ts 1`] = `/*eslint-disable*/import type{Messages}from"@lingui/core";export const messages=(JSON.parse("{\\"key\\":[\\"Hello \\",[\\"name\\"]]}")as Messages);`; exports[`createCompiledCatalog options.namespace should compile with window 1`] = `/*eslint-disable*/window.test={messages:JSON.parse("{\\"key\\":[\\"Hello \\",[\\"name\\"]]}")};`; diff --git a/packages/cli/src/api/compile.ts b/packages/cli/src/api/compile.ts index c1f398f0b..79f9f5dda 100644 --- a/packages/cli/src/api/compile.ts +++ b/packages/cli/src/api/compile.ts @@ -78,16 +78,16 @@ function buildExportStatement( ) importMessagesTypeDeclaration.importKind = "type" - // Create the exported `messages` identifier with a `Messages` TS type annotation - const messagesIdentifier = t.identifier("messages") - messagesIdentifier.typeAnnotation = t.tsTypeAnnotation( + // Cast the expression to `Messages` + const castExpression = t.tsAsExpression( + expression, t.tsTypeReference(t.identifier("Messages")) ) - // export const messages:Messages = { message: "Translation" } + // export const messages = ({ message: "Translation" } as Messages) const exportDeclaration = t.exportNamedDeclaration( t.variableDeclaration("const", [ - t.variableDeclarator(messagesIdentifier, expression), + t.variableDeclarator(t.identifier("messages"), castExpression), ]) )