Skip to content

Commit

Permalink
fix: include type cast on ts compilation (#1962)
Browse files Browse the repository at this point in the history
  • Loading branch information
tagleeson authored Jun 20, 2024
1 parent d8d437a commit 0f66617
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/api/__snapshots__/compile.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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\\"]]}")};`;

Expand Down
10 changes: 5 additions & 5 deletions packages/cli/src/api/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
])
)

Expand Down

0 comments on commit 0f66617

Please sign in to comment.