Feature flags
Modeling enabled or disabled configuration options.
TypeScript · Type Alias
The primitive boolean type represents the two values true and false. In TypeScript, boolean is equivalent to the union true | false; use lowercase boolean rather than boxed Boolean.
Use the lowercase boolean type for values that are either true or false. TypeScript treats boolean as the union of the two boolean literal types, true and false, while uppercase Boolean refers to a distinct boxed-object type that should generally be avoided.
booleantype FeatureFlags = {
darkMode: boolean;
betaSearch: boolean;
};
const flags: FeatureFlags = {
darkMode: true,
betaSearch: false,
};
if (flags.darkMode) {
console.log("dark mode enabled");
}The object uses the primitive boolean type, whose value can be true or false.
Modeling enabled or disabled configuration options.
Representing the outcome of a yes-or-no validation or permission check.
Discriminating union members with true and false literal properties.
The primitive boolean type represents exactly two values: true and false. TypeScript models it as the union true | false.
Follow-up: How would you require a property to be permanently true? Use the boolean literal type true rather than boolean.
Use lowercase boolean in annotations. Uppercase Boolean is legal but refers to a special boxed built-in type and is rarely appropriate in application code.
Follow-up: What analogous naming rule applies to other primitives? Prefer string and number over String and Number.
Boolean literals are the types true and false. They are useful when discriminating related object shapes, because a literal property can select a particular union member.
Follow-up: How can a boolean discriminant relate fields? Define separate union members with isValid: true and isValid: false, giving each member different accompanying properties.
truefalsestringnumberBooleanLiteral TypesEveryday Types