React / Redux / TypeScript Utils
npm install react-redux-typescript- Thoroughly tested for type correctness
- No third-party dependencies
- Semantic Versioning
- Output separate bundles for different workflow needs (es5-commonjs, es5-module, jsnext)
typesafe-actions directly instead!---
Archived docs:
- Docs v2.X
---
DiffKeys K and L and return a subset with a difference Usage:
``ts
import { OmitKeys } from 'react-redux-typescript';
interface BaseProps { a: string, b?: number, c: boolean }
interface Props { a: number, d: number }
type Diffed_Keys = DiffKeys
// Expect: 'b' | 'c'
`
> From set of keys K subtract it's subset K2 Usage:
`ts
import { OmitKeys } from 'react-redux-typescript';interface BaseProps { a: string, b?: number, c: boolean }
type Omitted_Keys = OmitKeys;
// Expect: 'b' | 'c'
`$3
> Diff
> From T remove intersecting properties with U Usage:
`ts
import { OmitKeys } from 'react-redux-typescript';interface BaseProps { a: string, b?: number, c: boolean }
interface Props { a: number, d: number }
type Diffed_Props = Diff;
// Expect { b?: number | undefined, c: boolean }
`$3
> Omit
> From T remove a set of properties K Usage:
`ts
import { OmitKeys } from 'react-redux-typescript';interface BaseProps { a: string, b?: number, c: boolean }
type Omitted_Props = Omit;
// Expect: { b?: number | undefined, c: boolean }
`$3
> Overwrite
> Replace intersecting properties from U to T Usage:
`ts
import { OmitKeys } from 'react-redux-typescript';interface BaseProps { a: string, b?: number, c: boolean }
interface Props { a: number, d: number }
type Overwritten_Props = Overwrite;
// Expect: { a: number, b?: number | undefined, c: boolean }
`$3
> Assign
> Copy and replace all properties from U to T Usage:
`ts
import { Assign } from 'react-redux-typescript';interface BaseProps { a: string, b?: number, c: boolean }
interface Props { a: number, d: number }
type Assigned_Props = Assign;
// Expect: { a: number, b?: number | undefined, c: boolean, d: number }
`---
Type Utils
$3
> Get return value of an "expression" with inferred return type
Alias: returntypeof
https://github.com/Microsoft/TypeScript/issues/6606 `ts
// this polyfill exist because TypeScript does not support getting type of expression
// (tracking issue: https://github.com/Microsoft/TypeScript/issues/6606)
function getReturnOfExpression(
expression: (...params: any[]) => T,
): T;// Example:
import { getReturnOfExpression } from 'react-redux-typescript';
const increment = () => ({ type: 'INCREMENT' as 'INCREMENT' });
const returnOfIncrement = getReturnOfExpression(increment);
type INCREMENT = typeof returnOfIncrement; // { type: "INCREMENT"; }
``---
MIT License
Copyright (c) 2016 Piotr Witek