Conditional types in TypeScript

JavascriptTypescriptTypes

Javascript Problem Overview


I was wondering if I can have conditional types in TypeScript?

Currently I have the following interface:

interface ValidationResult {
  isValid: boolean;
  errorText?: string;
}

But I want to remove errorText, and only have it when isValid is false as a required property.

I wish I was able to write it as the following interface:

interface ValidationResult {
  isValid: true;
}

interface ValidationResult {
  isValid: false;
  errorText: string;
}

But as you know, it is not possible. So, what is your idea about this situation?

Javascript Solutions


Solution 1 - Javascript

One way to model this kind of logic is to use a union type, something like this

interface Valid {
  isValid: true
}

interface Invalid {
  isValid: false
  errorText: string
}

type ValidationResult = Valid | Invalid

const validate = (n: number): ValidationResult => {
  return n === 4 ? { isValid: true } : { isValid: false, errorText: "num is not 4" }
}

The compiler is then able to narrow the type down based on the boolean flag

const getErrorTextIfPresent = (r: ValidationResult): string | null => {
  return r.isValid ? null : r.errorText
}

Solution 2 - Javascript

To avoid creating multiple interfaces which only get used to create a third, you can also alternate directly, with a type instead:

type ValidationResult = {
    isValid: false;
    errorText: string;
} | {
    isValid: true;
};

Solution 3 - Javascript

The union demonstrated by bugs is how I recommend handling this. Nonetheless, Typescript does have something known as “conditional types,” and they can handle this.

type ValidationResult<IsValid extends boolean = boolean> = (IsValid extends true
    ? { isValid: IsValid; }
    : { isValid: IsValid; errorText: string; }
);


declare const validation: ValidationResult;
if (!validation.isValid) {
    validation.errorText;
}

This ValidationResult (which is actually ValidationResult<boolean> due to the default parameter) is equivalent to the union produced in bugs’s answer or in CertainPerformance’s answer, and can be used in the same manner.

The advantage here is that you could also pass around a known ValidationResult<false> value, and then you wouldn’t have to test isValid as it would be known to be false and errorString would be known to exist. Probably not necessary for a case like this—and conditional types can be complex and difficult to debug, so they probably shouldn’t be used unnecessarily. But you could, and that seemed worth mentioning.

Solution 4 - Javascript

Here is an alternative approach where you don't need the isValid property. Instead we can use the presence or abscence of the errortext property as a marker instead. Here is an example:

// Empty for now, can always add properties to it
interface Valid{}

interface InValid {
    errorText: string;
}

// sum/union type, the type is either Valid OR InValid
type ValidationResult =  Valid | InValid;

// custom type guard to determine the type of the result
// TS uses this function to narrow down the type to eiter valid or invalid
function checkIfValidResult(result: ValidationResult): result is InValid{
    return result.hasOwnProperty('errorText') ? true : false;
}

// example of using the type guard
function doSomethingWithResult(result: ValidationResult) {
    if (checkIfValidResult(result)) {
        throw new Error(result.errorText);
    } else {
        console.log('Success!');
    }
}

doSomethingWithResult({});
// logs: Success

doSomethingWithResult({errorText:'Oops something went wrong'});
// Throws error: Oops something went wrong

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArmanView Question on Stackoverflow
Solution 1 - JavascriptbugsView Answer on Stackoverflow
Solution 2 - JavascriptCertainPerformanceView Answer on Stackoverflow
Solution 3 - JavascriptKRyanView Answer on Stackoverflow
Solution 4 - JavascriptWillem van der VeenView Answer on Stackoverflow