All Projects → kube → returnof

kube / returnof

Licence: other
TypeScript typeof function return helper

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to returnof

C-Complete-practice
This repository will contains C programs from beginners to advance level
Stars: ✭ 59 (+268.75%)
Mutual labels:  functions
effepi
Fun functional programming with pipelinable functions
Stars: ✭ 13 (-18.75%)
Mutual labels:  functions
fnmate
A function definition generator.
Stars: ✭ 74 (+362.5%)
Mutual labels:  functions
inventory-hub-java-on-azure
Sample Inventory Hub App using Serverless and Event-driven Java - on Azure with Spring Boot, Tomcat, Functions, Event Hub and Cosmos DB
Stars: ✭ 18 (+12.5%)
Mutual labels:  functions
openfaas-rstats-templates
OpenFaaS templates for R
Stars: ✭ 17 (+6.25%)
Mutual labels:  functions
gcf-packs
Library packs for google cloud functions
Stars: ✭ 48 (+200%)
Mutual labels:  functions
sequence-as-promise
Executes array of functions as sequence and returns promise
Stars: ✭ 23 (+43.75%)
Mutual labels:  functions
AzureFunctions-Workshop
Multi-language Azure Functions Workshop
Stars: ✭ 20 (+25%)
Mutual labels:  functions
fqn-check
Checks source trees for not fully qualified function calls.
Stars: ✭ 15 (-6.25%)
Mutual labels:  functions
openwhisk-runtime-dotnet
Apache OpenWhisk Runtime .Net supports Apache OpenWhisk functions written in .Net languages
Stars: ✭ 23 (+43.75%)
Mutual labels:  functions
ftrace
Simple Function calls tracer
Stars: ✭ 65 (+306.25%)
Mutual labels:  functions
openwhisk-runtime-python
Apache OpenWhisk Runtime Python supports Apache OpenWhisk functions written in Python
Stars: ✭ 39 (+143.75%)
Mutual labels:  functions
messaging
Conveniently issue messages, warnings, and errors
Stars: ✭ 12 (-25%)
Mutual labels:  functions
openwhisk-runtime-go
Apache OpenWhisk Runtime Go supports Apache OpenWhisk functions written in Go
Stars: ✭ 31 (+93.75%)
Mutual labels:  functions
Data-Science-Tutorials
Python Tutorials for Data Science
Stars: ✭ 104 (+550%)
Mutual labels:  functions
linbaser
The program on a given grid from a file builds an approximation of the function by a piecewise linear basis. Made mostly in Russian, because of university subject.
Stars: ✭ 10 (-37.5%)
Mutual labels:  functions
rudash
Rudash - Lodash for Ruby Apps
Stars: ✭ 27 (+68.75%)
Mutual labels:  functions
learning R
List of resources for learning R
Stars: ✭ 32 (+100%)
Mutual labels:  functions
ctxutil
utils for Go context
Stars: ✭ 18 (+12.5%)
Mutual labels:  functions
koryphe
A flexible library for writing functional operations in Java
Stars: ✭ 18 (+12.5%)
Mutual labels:  functions

returnof

A workaround to get the return type of a function in TypeScript.

DEPRECATED

Since TypeScript 2.8, and thanks to new Conditional Types and infer keyword, a ReturnType is now directly available in the language, permitting to get the return type of a function in a fully declarative way.

Usage

const hello = () => ({ hello: 'World' })

type HelloReturn = ReturnType<typeof hello>

Locked in the Past?

If for some strange reason you cannot upgrade to TypeScript 2.8+:

(Some teams stay locked in the past for a while)

It's gonna be really hard waiting 30 years before I can talk to you about everything that's happened in the past few days. I'm really gonna miss you, Marty.

– Dr. Emmett "Doc" Brown

Install

npm install returnof

Function return type

At the moment TypeScript does not allow to get the return type of a function.

const hello = () => ({ hello: 'World' })

type helloReturnType = typeof hello() // ERROR

returnof allows you to get it, at the cost of (a little) more verbosity.

import returnof from 'returnof'

const hello = () => ({ hello: 'World' })

const helloReturnValue = returnof(hello)
type helloReturnType = typeof helloReturnValue // { hello: string }

At runtime, helloReturnValue will be null.

Overloaded functions

If your function is overloaded and there is an ambiguity on which one you're trying to get the return type, you can pass additional arguments:

declare function hello(): void;
declare function hello(a: number): number;

const helloReturnValue = returnof(hello)
type helloReturnType = typeof helloReturnValue // void

const helloReturnValue = returnof(hello, 42)
type helloReturnType = typeof helloReturnValue // number

If there's no ambiguity, you do not need to pass additional arguments:

declare function hello(a: number): number;

const helloReturnValue = returnof(hello)
type helloReturnType = typeof helloReturnValue // number
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].