TypeScript utility types for extracting common properties from multiple types.
This module provides two main approaches to finding common properties:
interface Cat { name: string; type: 'cat'; active: true; }
interface Dog { name: string; type: 'dog'; active: false; }
// Strict: only exact matches
type StrictCommon = CommonStrictProps<[Cat, Dog]>; // { name: string }
// Upcast: literals become primitives
type UpcastCommon = CommonUpcastProps<[Cat, Dog]>; // { name: string; type: string; active: boolean }
CommonStrictProps<T[]> - Find common properties with strict type matchingCommonUpcastProps<T[]> - Find common properties with primitive literal upcastingIsUpcastable<T> - Check if a type can be upcast to its primitive baseGetUpcastable<T> - Get the primitive base type for a given typeCommonStrictPairs<T, U> - Two-type strict comparisonCommonUpcastPairs<T, U> - Two-type upcast comparisonProperties are completely excluded when they cannot be unified:
interface A { name: string; type: 'cat'; }
interface B { name: string; type: 'dog'; }
type Strict = CommonStrictProps<[A, B]>;
// Result: { name: string }
// 'type' property is excluded entirely (not present in result type)
When types can be unified, more general types are preferred:
// Example: string vs 'literal'
type Mixed = CommonUpcastPairs<{prop: string}, {prop: 'literal'}>;
// Result: {prop: string} - chooses the more general type
// Multi-step example:
interface Cat { type: 'cat' }
interface Dog { type: 'dog' }
interface Bird { type: 'bird' }
type Step1 = CommonUpcastPairs<Cat, Dog>; // {type: string}
type Step2 = CommonUpcastPairs<Step1, Bird>; // {type: string} (maintains generality)
Multi-type functions process types left-to-right, maintaining type generality:
type Result = CommonUpcastProps<[Cat, Dog, Bird]>;
// Equivalent to: CommonUpcastPairs<CommonUpcastPairs<Cat, Dog>, Bird>
.gitignoreThis project is licensed under the Apache License 2.0 - see the LICENSE.txt file for details.
Copyright 2025 Adam Mill