Axel Rauschmayer on Nostr: #TypeScript experiment: dropping elements from a tuple type Drop<Tuple extends ...
#TypeScript experiment: dropping elements from a tuple
type Drop<Tuple extends Array<unknown>, Num extends number, Counter extends Array<boolean> = []> =
Counter['length'] extends Num
? Tuple
: Tuple extends [unknown, ...infer Rest extends Array<unknown>]
? Drop<Rest, Num, [true, ...Counter]>
: Tuple
;
type X = Drop<['a', 'b', 'c'], 2>; // ["c"]
`Counter` feels hacky but not sure it can be avoided.
Published at
2025-01-25 17:16:55 UTCEvent JSON
{
"id": "d8a475084e661244b4eae42d85bc1042902c2b3d05791bff7870e928301be534",
"pubkey": "7a7a0978cc46ef7fd548fb125e51c04de7d4ca639417a21b56aa95e3711c0b38",
"created_at": 1737825415,
"kind": 1,
"tags": [
[
"t",
"typescript"
],
[
"proxy",
"https://fosstodon.org/@rauschma/113890126400385256",
"web"
],
[
"proxy",
"https://fosstodon.org/users/rauschma/statuses/113890126400385256",
"activitypub"
],
[
"L",
"pink.momostr"
],
[
"l",
"pink.momostr.activitypub:https://fosstodon.org/users/rauschma/statuses/113890126400385256",
"pink.momostr"
],
[
"-"
]
],
"content": "#TypeScript experiment: dropping elements from a tuple\n\ntype Drop\u003cTuple extends Array\u003cunknown\u003e, Num extends number, Counter extends Array\u003cboolean\u003e = []\u003e =\n Counter['length'] extends Num\n ? Tuple\n : Tuple extends [unknown, ...infer Rest extends Array\u003cunknown\u003e]\n ? Drop\u003cRest, Num, [true, ...Counter]\u003e\n : Tuple\n;\ntype X = Drop\u003c['a', 'b', 'c'], 2\u003e; // [\"c\"]\n\n`Counter` feels hacky but not sure it can be avoided.",
"sig": "ba29cf5a15c7fde1c7610f5799fda60c93452569a18018bf9e227dbc239475c174b594b701a5660b67f6fe75ff4c9e2465354a0ee3b2079887155f8fe07cc670"
}