Socket
Socket
Sign inDemoInstall

@arthurka/ts-utils

Package Overview
Dependencies
8
Maintainers
1
Versions
57
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @arthurka/ts-utils

Some convenient TS utilities.


Version published
Weekly downloads
449
increased by0.22%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

Usage

import { checkExactShape, ExactShape } from '@arthurka/ts-utils';

const tooFew = { name: 'Stefan' };
const exact = { name: 'Stefan', age: '31' };
const tooMany = { name: 'Stefan', age: '31', sex: 'male' };

const record: Record<string, string> = {};
type R = typeof record;
type Exact = typeof exact;
interface ExtendedRecord extends Exact, R {}
const extendedRecord: ExtendedRecord = {
  name: 'Stefan',
  age: '31',
  sex: 'male',
};

type Person = {
  name: string;
  age: string;
};


function savePerson<T extends Person>(e: ExactShape<Person, T>) {

}

savePerson(exact);
// @ts-expect-error
savePerson(tooFew);
// @ts-expect-error
savePerson(tooMany);
// @ts-expect-error
savePerson(record);
// @ts-expect-error
savePerson(extendedRecord);

function getPerson(): Person {
  const e = exact;
  // const e = tooFew;
  // const e = tooMany;
  // const e = record;
  // const e = extendedRecord;

  checkExactShape<Person>()(e);
  return e;
}

const person = getPerson();
console.info(person);

FAQs

Last updated on 12 Mar 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc