Removing duplication with ternary operators
The ternary operator can be used at many levels. The further we move it into a statement, the more duplication it can save us.
Read full articleWe can define a type in which only one of two mutually exclusive properties should exist.
We first need to create a base interface that holds all properties that always exist. We then extend new interfaces from that base, one for each of the exclusive variants.
In each of these, we then set one of the properties to a regular type and the other to optional (?
) and never
.
Our final type is the union of the two variant interfaces.
interface Base {
// all shared properties
}
interface VariantA extends Base {
one: string
two?: never
}
interface VariantB extends Base {
one?: never
two: string
}
type Thing = VariantA | VariantB
interface Base {
// all shared properties
}
interface VariantA extends Base {
one: string
two?: never
}
interface VariantB extends Base {
one?: never
two: string
}
type Thing = VariantA | VariantB
interface Base {
// all shared properties
}
interface VariantA extends Base {
one: string
two?: never
}
interface VariantB extends Base {
one?: never
two: string
}
type Thing = VariantA | VariantB
interface Base {
// all shared properties
}
interface VariantA extends Base {
one: string
two?: never
}
interface VariantB extends Base {
one?: never
two: string
}
type Thing = VariantA | VariantB
We can then use that type for both variants. Setting either one of the mutually exclusive properties is fine, setting both isn’t.
// this is fine
const a: Thing = {
one: "hello",
}
// this is also fine
const b: Thing = {
two: "world",
}
// this is not allowed
const c: Thing = {
one: "hello",
two: "world",
}
// this is fine
const a: Thing = {
one: "hello",
}
// this is also fine
const b: Thing = {
two: "world",
}
// this is not allowed
const c: Thing = {
one: "hello",
two: "world",
}
// this is fine
const a: Thing = {
one: "hello",
}
// this is also fine
const b: Thing = {
two: "world",
}
// this is not allowed
const c: Thing = {
one: "hello",
two: "world",
}
// this is fine
const a: Thing = {
one: "hello",
}
// this is also fine
const b: Thing = {
two: "world",
}
// this is not allowed
const c: Thing = {
one: "hello",
two: "world",
}
The ternary operator can be used at many levels. The further we move it into a statement, the more duplication it can save us.
Read full articleWe can turn arrays of absolute numbers into relative numbers. The largest value becomes 1, with the others calculated based on that largest value.
Read full articleInstead of passing a list of parameters to a function, we can pass it an object. That way, we have to explicitly label each parameter we pass to it.
Read full articleCombining the map function on arrays and type constructors allows us to quickly map values from one type to another.
Read full articleInstead of splitting strings at sequences of characters, we can split them into substrings of equal length by using a regular expression.
Read full articleTo improve the readability of React components, we can import the styled-components they use from another file under a ui-namespace.
Read full article