1367 - Remove Index Signature
原创2025/4/30小于 1 分钟
1367 - Remove Index Signature
by hiroya iizuka (@hiroyaiizuka) #中等 #object-keys
题目
Implement RemoveIndexSignature<T>
, exclude the index signature from object types.
For example:
type Foo = {
[key: string]: any
foo(): void
}
type A = RemoveIndexSignature<Foo> // expected { foo(): void }
在 Github 上查看:https://tsch.js.org/1367/zh-CN
代码
/* _____________ 你的代码 _____________ */
type RemoveIndexSignature<T, P = PropertyKey> = {
[K in keyof T as P extends K ? never : K extends P ? K : never]: T[K]
}
测试用例
/* _____________ 测试用例 _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type Foo = {
[key: string]: any
foo(): void
}
type Bar = {
[key: number]: any
bar(): void
0: string
}
const foobar = Symbol('foobar')
type FooBar = {
[key: symbol]: any
[foobar](): void
}
type Baz = {
bar(): void
baz: string
}
type cases = [
Expect<Equal<RemoveIndexSignature<Foo>, { foo(): void }>>,
Expect<Equal<RemoveIndexSignature<Bar>, { bar(): void, 0: string }>>,
Expect<Equal<RemoveIndexSignature<FooBar>, { [foobar](): void }>>,
Expect<Equal<RemoveIndexSignature<Baz>, { bar(): void, baz: string }>>,
]
相关链接
分享你的解答:https://tsch.js.org/1367/answer/zh-CN
查看解答:https://tsch.js.org/1367/solutions
更多题目:https://tsch.js.org/zh-CN