Skip to content

This is a small utilty type that helps you build objects with keys which are dependant on other objects

Notifications You must be signed in to change notification settings

shacharRonZohar/DependantObjectType

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dependant Object Keys (Name WIP)

This is a small utility type, which allows you to create one way "dependancies", or "constraints" between keys in diffrent objects.

Currently the type only supports boolean => any dependancies, but I plan to support any => any dependancies in the future.

Installation

Install dependant-object-keys:

  • with pnpm
  pnpm install dependant-object-keys
  • with npm
  npm install dependant-object-keys
  • with yarn
   yarn add dependant-object-keys

Usage/Examples

To import the type, use import type like so:

import type { DependantObj } from 'dependant-object-keys'

Then define the interface to use as the allowed values (you can use one of your existing interfaces / data structures),

and the object defining which keys are supposed to exist, it should be extandable to the following type:

interface BooleanIndex {
  [key: string]: boolean
}

(again, you can use on of your existing data structures)

interface AnyIndex {
  [key: string | symbol | number]: any
}

interface TestKeyMap1 extends AnyIndex {
  foo: 'fai' | 'fom'
  bar: 'bir' | 'bor'
}

const testMap1 = {
  isFoo: true,
  isBar: false,
} as const // We use as const to ensure the type signature is exact with the values in the object

You don't have to use "is" as a prefix, but you should know the type unpacks that, and the resuling type def will be whatever name was in the testMap1 object, without the "is", if it exists, so for testMap1 the resulting type def object will look like this:

type ResType = {
  foo: 'fai' | 'fom'
}

The same ResType type will be generated for the following testMap:

const testMapSame = {
  foo: true,
  bar: false,
}

Then we can define the actual constrained object, here are several examples of expected outcomes:

const obj1 = {
  foo: 'fai',
  bar: 'bir', // will give TS error -  Object literal may only specify known properties,
  // and 'syntax' does not exist in type DependantObj<typeof testMap1, TestKeyMap>
} satisfies DependantObj<typeof testMap1, TestKeyMap>

const testMap2 = {
  isFoo: true,
  isBar: true,
} as const

const obj2 = {
  foo: 'hi', // will give TS error - Type '"hi"' is not assignable to type '"fai" | "fom"'.
} satisfies DependantObj<typeof testMap2, TestKeyMap> // will also give TS error - Property 'bar' is missing in type '{ foo: "fai"; }',
// but required in type DependantObj<typeof testMap2, TestKeyMap>

Authors

Acknowledgements

  • Tom Bachar - who sent me on the right direction for solving this problem.

  • Noy Morgenshtein - which helped me understand typescripts conditional types.

Feedback

If you have any feedback, please reach out to me @ [email protected], or feel free to open up a github issue / discussion for the repo!

Contributing

This is a simple project in it's baby stages, so any and all contributions are always welcome!

About

This is a small utilty type that helps you build objects with keys which are dependant on other objects

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published