Tuple.map: accept ordinary callbacks while preserving tuple length
メンテナーはふだん 1 日以内に返信
評価
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 72/100
- issue の種類
- 機能追加
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- typescript
調査の方向性
Start with Tuple.map in the tuple module and the existing overloads; the issue specifies adding ordinary callback overloads after the Lambda overloads, with no runtime change. Read the type tests in packages/effect/typetest/Tuple.tst.ts and runtime tests in packages/effect/test/Tuple.test.ts, then run them. Done when the listed tuple type behaviors pass and Lambda behavior remains intact.
索引モデルが issue の本文から書いたものです。
説明
Problem
Tuple.map only accepts a Struct.Lambda. Mapping a fixed-length tuple with an ordinary function is rejected, and the only way to keep the length today is to declare a Lambda interface for a one-line callback:
import { Tuple } from "effect"
const xs = [1, 2, 3] as [number, number, number]
// Error: '(n: number) => number' does not satisfy 'Struct.Lambda'
Tuple.map(xs, (n) => n + 1)
// Works, but needs an interface + Struct.lambda for a trivial callback
interface Inc extends Struct.Lambda {
(n: number): number
readonly "~lambda.out": number
}
Tuple.map(xs, Struct.lambda<Inc>((n) => n + 1)) // [number, number, number]
Array.map(xs, (n) => n + 1) works but returns Array<number>, losing the length. Requested by TheStockBroker on Discord.
Accepted design
Add ordinary-callback overloads to Tuple.map, placed after the existing Lambda overloads. The callback receives the element union T[number] and every position in the result becomes the callback's return type, through the same homomorphic mapped type the Lambda overloads already use:
export const map: {
// existing Lambda overloads, unchanged and first
<L extends Lambda>(lambda: L): <const T extends ReadonlyArray<unknown>>(self: T) => { [K in keyof T]: Apply<L, T[K]> }
<const T extends ReadonlyArray<unknown>, L extends Lambda>(self: T, lambda: L): { [K in keyof T]: Apply<L, T[K]> }
// new
<A, B>(f: (a: A) => B): <const T extends ReadonlyArray<A>>(self: T) => { [K in keyof T]: B }
<const T extends ReadonlyArray<unknown>, B>(self: T, f: (a: T[number]) => B): { [K in keyof T]: B }
}
No runtime change: the implementation is already self.map((e) => lambda(e)).
Behaviour
Tuple.map([1, 2, 3] as [number, number, number], (n) => n + 1)is[number, number, number].pipe(xs, Tuple.map((n: number) => n + 1))is[number, number, number]. As withArray.map, the data-last callback needs an annotated parameter; an unannotated one isunknown.- Heterogeneous tuples work:
Tuple.map(["a", 2, true] as [string, number, boolean], (x) => String(x))is[string, string, string], and the callback parameter isstring | number | boolean. A callback whose parameter is narrower than the element union is rejected. - The homomorphic mapped type keeps
readonly, optional slots ([string?, number?]maps to[B?, B?]), rest elements, and the empty tuple. - Literals are not falsely preserved:
Tuple.map([1, 2, 3] as const, (n) => n + 1)isreadonly [number, number, number](readonlybecauseconst Tinfers the literal as a readonly tuple, matching the existing Lambda overload). - Lambda behaviour is unchanged.
Tuple.map(schemas, Schema.NullOr)still tracks each position separately.
Decisions
Element union instead of a Homogeneous<T> guard. The Discord sketch proposed a type-level check that every slot equals T[number]. It is not needed for soundness: the callback is called with each element, every element is in T[number], and every result slot is B. The guard would only reject sound programs such as the heterogeneous example above and add a conditional type to every call. Dropped.
Lambda overloads stay first. The two data-first overloads share arity 2, so order matters. Callback-first breaks Lambdas: a generic Lambda such as Schema.NullOr satisfies (a: T[number]) => B, so TypeScript picks the callback overload and collapses every slot to NullOr<String | Number | Boolean>. Lambda-first does not have the mirror problem: a plain arrow does not satisfy the Lambda constraint, so it falls through to the callback overload, and the arrow's parameter is still contextually typed from T[number] (verified on TypeScript 5.9, 6.0 and 7.1: identity callbacks infer [number, number, number], not any).
Alternatives probed and rejected: callback-first with a & { readonly "~lambda.in"?: never } guard on the callback parameter (works, but encodes a Lambda internal into the public signature), and a single signature with F extends Lambda | ((a: T[number]) => unknown) and a conditional result type (works, but hides which path applies and worsens error messages). Both pass the same probe as the accepted design with no advantage.
Out of scope
Struct.map, Tuple.mapPick and Tuple.mapOmit have the same Lambda-only restriction. Same pattern would apply; separate issue if wanted.
Tests
Type tests in packages/effect/typetest/Tuple.tst.ts and a runtime test in packages/effect/test/Tuple.test.ts cover the behaviour above and currently fail on the type side.
- 主要言語
- TypeScript
- スター
- 16.7k
- フォーク
- 808
- 平均マージ
- 17時間 9分
- マージ済み PR(30日)
- 825
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
Effect-TS/effect のほかの issue
-
Arbitrary.schema: no generation override for a struct's cross-field filter, and lookahead patterns are skipped quietly対応中かも @effect-bot が 1 日前に担当しました。 オープン
Effect-TS/effect#10479 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
`Cookies` drops same-name cookies set for a different domain or path対応中かも @effect-bot が 2 日前に担当しました。 オープン
Effect-TS/effect#10469 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
Schema: a public way to derive a canonical codec, as `toCodecJson` and `toCodecStringTree` do対応中かも @effect-bot が 3 日前に担当しました。 オープンNeeds Proposal schema
Effect-TS/effect#10457 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
McpServer.layerHttp: optional legacy GET SSE for idle list-change notifications対応中かも @effect-bot が 3 日前に担当しました。 オープン
Effect-TS/effect#10436 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
McpServer.layerStdio: native EOF cancellation policy for active tool handlers対応中かも @effect-bot が 5 日前に担当しました。 オープン
Effect-TS/effect#10412 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
Effect-TS/effect の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
home-assistant/frontend#55841 ·
メンテナーはふだん 1 日以内に返信
-
community first-timers-only good first issue hacktoberfest help wanted low hanging fruit up-for-grabs
難易度 1/5 1時間未満 初心者へのやさしさ 76/100
lingdojo/kana-dojo#33905 · コメント 1 件 · リアクション 5 件 ·
メンテナーはふだん 1 日以内に返信
-
bug(semester-planning): the edit application dialog cannot save with an empty «Annet fra bedriften»オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
blocklist removal
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
MetaMask/eth-phishing-detect#311215 ·
メンテナーはふだん 1 日以内に返信
-
Explore filter: multi-select value list with allowNewOptions stops accepting values and drops earlier ones対応中かも @isaac-jaynes-imperva が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
apache/superset#46146 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信