1.0.0 • Published 2 years ago
keyword-args v1.0.0
keyword-args
Python style keyword arguments with thorough typescript typing inference.
Examples
A trivial example:
import { kwargs, KwsParameters, extractKwargs } from 'keyword-args';
function add(...args: KwsParameters<[a: number, b: number], { type: 'number' }>): number;
function add(...args: KwsParameters<[a: number, b: number], { type: 'string' }>): string;
function add(
...args: KwsParameters<[a: string | number, b: string | number], { type: 'number' | 'string' }>
): number | string {
const [[a, b], { type }] = extractKwargs(args, { type: 'number' });
if (type === 'number') return +a + +b;
return `${a}${b}`;
}
const a = add(1, 2, { type: 'number' }) // type: number
const b = add('1', '2', { type: 'string' }) // type: string
Spread arguments:
function python_print(
...args: SpreadKwsParameters<
string[],
{ sep?: string; end?: string }
>): void {
const [strs, { sep, end }] = extractKwargs(args, { sep: ' ', end: '\n' });
console.log(strs.join(sep) + end);
}
python_print('hello', 'world', kwargs({ sep: '\n', end: '' }));
// hello
// world
TODO
- Typing for default kwargs
- Unify
SpreadKwsParameters
andKwsParameters
- Tests
- More examples
1.0.0
2 years ago