You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I searched existing ideas and did not find a similar one
I added a very descriptive title
I've clearly described the feature request and motivation for it
Feature request
There are multiple places in the codebase where conditional branching does not include compile-time exhaustive checks.
The TypeScript pattern allows for early warnings directly through the IDE when a new conditional case is added or modified and is not addressed accordingly in a long switch statement or if/else/if branching.
I believe this to be a really useful pattern for new or modified cases in branching conditionals that might remain unhandled, especially in a codebase like this which will definitely introduce additional cases in multiple places in the future. It's an extra layer of stability and maintainability since it catches such problems before runtime has a chance to produce an error and a tester/user notices it.
For example, in langchain/src/chains/base.ts in the deserialize function, through applying exhaustive checks, we immediately see that 3 cases are not handled (probably something intentional, but nevertheless nice to see):
TypeScript complains: Type '"chat-vector-db" | "analyze_document_chain" | "constitutional_chain"' is not assignable to type 'never'
Proposal (If applicable)
Here's an example:
// Path: langchain/src/retrievers/hyde.tsexporttypePromptKey=|"websearch"|"scifact"|"arguana"|"trec-covid"|"fiqa"|"dbpedia-entity"|"trec-news"|"new_type"// Let's add a new case and forget to address it below...|"mr-tydi";/** * Returns a BasePromptTemplate instance based on a given PromptKey. */exportfunctiongetPromptTemplateFromKey(key: PromptKey): BasePromptTemplate{lettemplate: string;switch(key){case"websearch":
template=`Please write a passage to answer the questionQuestion: {question}Passage:`;break;case"scifact":
template=`Please write a scientific paper passage to support/refute the claimClaim: {question}Passage:`;break;case"arguana":
template=`Please write a counter argument for the passagePassage: {question}Counter Argument:`;break;case"trec-covid":
template=`Please write a scientific paper passage to answer the questionQuestion: {question}Passage:`;break;case"fiqa":
template=`Please write a financial article passage to answer the questionQuestion: {question}Passage:`;break;case"dbpedia-entity":
template=`Please write a passage to answer the question.Question: {question}Passage:`;break;case"trec-news":
template=`Please write a news passage about the topic.Topic: {question}Passage:`;break;// FORGET TO ADDRESS A CASE:// case "mr-tydi":// template = `Please write a passage in Swahili/Korean/Japanese/Bengali to answer the question in detail.// Question: {question}// Passage:`;// break;default:
// ERROR: Type '"new_type" | "mr-tydi"' is not assignable to type 'never'.const_exhaustive_check: never=key;// <= Exhaustive check will give us an early warning immediately in our IDEthrownewError(`Invalid prompt key: ${key}`);}returnPromptTemplate.fromTemplate(template);}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Checked
Feature request
There are multiple places in the codebase where conditional branching does not include compile-time
exhaustive checks
.The TypeScript pattern allows for early warnings directly through the IDE when a new conditional case is added or modified and is not addressed accordingly in a long switch statement or if/else/if branching.
For example:
langchainjs/langchain/src/util/axios-fetch-adapter.js
Line 284 in d303e90
langchainjs/langchain/src/chains/base.ts
Line 275 in d303e90
langchainjs/langchain/src/evaluation/loader.ts
Line 61 in d303e90
Motivation
I believe this to be a really useful pattern for new or modified cases in branching conditionals that might remain unhandled, especially in a codebase like this which will definitely introduce additional cases in multiple places in the future. It's an extra layer of stability and maintainability since it catches such problems before runtime has a chance to produce an error and a tester/user notices it.
For example, in
langchain/src/chains/base.ts
in thedeserialize
function, through applying exhaustive checks, we immediately see that 3 cases are not handled (probably something intentional, but nevertheless nice to see):TypeScript complains:
Type '"chat-vector-db" | "analyze_document_chain" | "constitutional_chain"' is not assignable to type 'never'
Proposal (If applicable)
Here's an example:
Beta Was this translation helpful? Give feedback.
All reactions