Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

typed-function

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typed-function

Type checking for JavaScript functions

  • 0.6.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.1M
increased by20.36%
Maintainers
1
Weekly downloads
 
Created

What is typed-function?

The typed-function npm package allows you to define functions with typed arguments in JavaScript. It provides a way to enforce type checking at runtime, making your code more robust and easier to debug.

What are typed-function's main functionalities?

Define a typed function

This feature allows you to define a function with multiple signatures, each with different types of arguments. The function will execute the appropriate implementation based on the types of the provided arguments.

const typed = require('typed-function');

const add = typed({
  'number, number': function (a, b) {
    return a + b;
  },
  'string, string': function (a, b) {
    return a + b;
  }
});

console.log(add(2, 3)); // 5
console.log(add('Hello, ', 'world!')); // 'Hello, world!'

Type checking

This feature enforces type checking at runtime, throwing an error if the provided arguments do not match any of the defined signatures. This helps catch type-related bugs early in the development process.

const typed = require('typed-function');

const multiply = typed({
  'number, number': function (a, b) {
    return a * b;
  }
});

try {
  console.log(multiply(2, '3')); // Throws an error
} catch (err) {
  console.error(err.message); // 'TypeError: Unexpected type of argument in function multiply (expected: number, actual: string, index: 1)'
}

Default types

This feature allows you to define default types for your function arguments. If no arguments are provided or if they do not match any specific type, the function will fall back to the default implementation.

const typed = require('typed-function');

const greet = typed({
  'string': function (name) {
    return 'Hello, ' + name + '!';
  },
  'any': function () {
    return 'Hello, world!';
  }
});

console.log(greet('Alice')); // 'Hello, Alice!'
console.log(greet()); // 'Hello, world!'

Other packages similar to typed-function

Keywords

FAQs

Package last updated on 08 Mar 2015

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc