35191-medium-trace
Medium

チャレンジのソース: type-challenges/35191-medium-trace

行列において、トレースは主対角成分の総和です。 それを型システムを使って求めたいです。 しかし、型で数を足すのは難しいため、代わりに主対角成分のユニオン型を求めてください。

例:

type Arr = [
  [1,2],
  [3,4]
]
type Test = Trace<Arr> // expected to be 1 | 4

Loading...
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<Trace<[[1, 2], [3, 4]]>, 1 | 4>>,
  Expect<Equal<Trace<[[0, 1, 1], [2, 0, 2], [3, 3, 0]]>, 0>>,
  Expect<Equal<Trace<[['a', 'b', ''], ['c', '', ''], ['d', 'e', 'f']]>, 'a' | '' | 'f'>>,
]