Here's a type I'm working with:
type EmbeddingsSummary = { [path: string]: Embedding | WontGetEmbeddingReason }
WontGetEmbeddingReason
is a string enum, so I expected that
(typeof embeddings[relativePath] === 'string') ? undefined :
getCosineSimilarity(embeddings[relativePath], searchEmbedding)
will work fine (to be clear: Embedding
is an array). However (with getCosineSimilarity
expecting Embedding
as the first argument), TypeScript complains:
Argument of type 'Embedding | WontGetEmbeddingReason' is not assignable to parameter of type 'Embedding'.
so in fact I'm currently using a mediocre workaround (casting):
(typeof embeddings[relativePath] === 'string') ? undefined :
getCosineSimilarity(embeddings[relativePath] as Embedding, searchEmbedding)
Why TS doesn't deduce that if typeof embeddings[relativePath] === 'string'
then embeddings[relativePath]
is actually an Embedding
? How can I fix this check?