1.0.0 • Published 9 months ago

@just-web-tech/get-set v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

@just-web-tech/get-set

Type safe get and set properties using dot notation

Features

  • 🔑 Type safe
  • ⚡️ No dependencies
  • 📦 Tree shakeable
  • 🔥 < 1 kb size (gzip)

Install

npm i @just-web-tech/get-set

Usage

Safely getting and setting properties via dot notation

const project: Project = {
  name: '@just-web-tech/get-set',
  created: new Date(),
  test: {
    framework: 'vitest',
  },
  files: [{ folder: 'dist' }]
};

const file = getValue(project, 'files.0.folder'); // 'dist'
setValue(project, 'files.0.folder', 'build'); // project.files is equal to ['build']

Utility types

Path<T>

Returns a string representation of the paths for all properties

type Paths = Path<Project>;
// "name" | "created" | "test" | "files"
// "test.framework" | `files.${number}` | `files.${number}.folder`

PathValue<T, P>

Returns the type of the property at the specified path

type Created = PathValue<Project, 'created'>; // Date