CommonProps Documentation - v0.1.2
    Preparing search index...

    CommonProps Documentation - v0.1.2

    CommonProps

    TypeScript utility types for extracting common properties from multiple types.

    This module provides two main approaches to finding common properties:

    • Strict mode: Properties must have exactly matching types
    • Upcast mode: Primitive literals can be upcast to their base types (e.g., 'hello' → string)
    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 }

    API documentation

    • CommonStrictProps<T[]> - Find common properties with strict type matching
    • CommonUpcastProps<T[]> - Find common properties with primitive literal upcasting
    • IsUpcastable<T> - Check if a type can be upcast to its primitive base
    • GetUpcastable<T> - Get the primitive base type for a given type
    • CommonStrictPairs<T, U> - Two-type strict comparison
    • CommonUpcastPairs<T, U> - Two-type upcast comparison

    Properties 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>
    • This project uses a whitelist approach to .gitignore

    This project is licensed under the Apache License 2.0 - see the LICENSE.txt file for details.

    Copyright 2025 Adam Mill