ksxnodemodules / typescript-tuple Goto Github PK
View Code? Open in Web Editor NEWGenerics to work with tuples in TypeScript
Home Page: https://www.npmjs.com/package/typescript-tuple
License: MIT License
Generics to work with tuples in TypeScript
Home Page: https://www.npmjs.com/package/typescript-tuple
License: MIT License
Since TypeScript 3.4, it refuses to do recursive operation. Must fix it somehow.
The Drop
type has an infinite instantiation when used in a generic
Drop<any[], number>; // This throws an "infinite instantiation" error
Drop<[], number>; // The first parameter is specific, the second generic, this is OK
Drop<any[], 0>; // The first parameter is generic, the second specific, this is also OK
Is there a specific reason why the package doesn't provide implementations for basic operations? Are there any plans for adding them in the future? I ended up writing some implementations for these types since I needed to work with tuples. Perhaps the types could be contributed to fp-ts Tuple module if they had an implementation? See gcanti/fp-ts#1074
I have this tuple of functions and I am trying to reverse it.
namespace Utils {
export type IsFinite<Tuple extends any[], Finite, Infinite> = {
empty: Finite
nonEmpty: ((..._: Tuple) => any) extends ((_: infer First, ..._1: infer Rest) => any)
? IsFinite<Rest, Finite, Infinite>
: never
infinite: Infinite
}[
Tuple extends [] ? 'empty' :
Tuple extends (infer Element)[] ?
Element[] extends Tuple ?
'infinite'
: 'nonEmpty'
: never
]
export type Prepend<Tuple extends any[], Addend> =
((_: Addend, ..._1: Tuple) => any) extends ((..._: infer Result) => any) ? Result : never
export type Reverse<Tuple extends any[], Prefix extends any[] = []> = {
empty: Prefix,
nonEmpty: ((..._: Tuple) => any) extends ((_: infer First, ..._1: infer Next) => any)
? Reverse<Next, Prepend<Prefix, First>>
: never
infinite: {
ERROR: 'Cannot reverse an infinite tuple'
CODENAME: 'InfiniteTuple'
}
}[
Tuple extends [any, ...any[]]
? IsFinite<Tuple, 'nonEmpty', 'infinite'>
: 'empty'
]
}
export type ReadFuncs = readonly ((x: any) => any)[]
export type Reverse<Tuple extends ReadFuncs> = Utils.Reverse<Tuple>
With TS 3.4 it became common practice to create tuples with [a, b, c] as const
. This is really convenient, however the resulting tuple is readonly
. It would be awesome to add support for these readonly tuples, right now I get error readonly (something) is not assignable to any[]
.
warning [email protected]: The engine "typescript" appears to be invalid.
Currently I'm getting this issue because you declare here:
"engines": { "typescript": "^3.0.0" }
in the package.json file.
https://docs.npmjs.com/files/package.json#engines
Please take a look at this official documentation, apparently TS should not be used here.
This library was added to my project after updating another library dependency. I am using v2.2.1. I get a Type expected
error from this library when building my source on the following lines:
typescript-tuple/lib/index.d.ts(19,47)
typescript-tuple/lib/index.d.ts(156,60)
typescript-tuple/lib/index.d.ts(161,59)
typescript-tuple/lib/utils.d.ts(13,87)
typescript-tuple/lib/utils.d.ts(64,23)
What causes this? Is the issue fixed in a newer release?
Ive recently been looking for ages to find a type-save implementation of recursively plucking an object via a tuple (explaination below). As I was unable to find any solution online, I resorted to posting a stackoverflow question which was answered brilliantly by @maciejsikora very recently.
Would you be open to adopting this type here? Before diving in to the depths of the internet to find a solution for my problem, I was aware of typescript-tuple
(searching typescript-tuple
was actually the first thing I did when researching). Hence my request to adopt it here. I understand if you consider this off topic here, since it is not purely of tuple domain. If so, Id gald if you could point me in a direction (maybe another library) where this feature would be better suited.
type Test1 = RecursivePluck<{a: {b: {c: {e: string}}, d: string}}, ['a','d']> // typeof string
type Test2 = RecursivePluck<{a: {b: {c: string}, d: string}},['a', 'b']> // typeof {c: string}
Here the implemantation by @maciejsikora.
This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.
These updates have all been created already. Click a checkbox below to force a retry/rebase of any.
package.json
typescript-compare ^0.0.2
typescript ~4.2.0
tslint ^6.1.3
tslint-config-standard ^9.0.0
static-type-assert ^4.0.1
toolcheck ^0.1.4
clean-typescript-build ^0.1.5
.travis.yml
node 10
https://github.com/eigenmethod/mol/tree/master/type
type should_be_same_but_without_head = Assert<
Tail< [ 666 , 777 ] > ,
[ 777 ]
>
Can I somehow write a function "append" that takes a random tuple and appends something to it?
Example:
append([1,'2',3], true) // should be [1,'2',3,true]
append([], 1) // should be [1]
I have tried something like this:
function append<A extends any[], V>(a: A, v: V): Append<A, V>;
but I keep receiving "Infinite Tuple" Error.
I was wondering if you could help me.
I attempted implementing reverse. For some reason the types work sometimes in a straight forward manner ( see reverse1
below ) and sometimes backwards ( see reverse2
). Is this a bug or is this how it is supposed to work?
function reverse1<A extends Array<any>>(a: A): Reverse<A> {
return (a.reverse() as unknown) as Reverse<A>;
}
function reverse2<A extends Array<any>>(a: Reverse<A>): A {
return (a.reverse() as unknown) as A;
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.