RemoveIndexSignature
介绍
实现RemoveIndexSignature<T>
, 从对象类型中排除索引签名
例如:
ts
type Foo = {[key: string]: anyfoo(): void}type A = RemoveIndexSignature<Foo> // expected { foo(): void }
View on GitHubts
type Foo = {[key: string]: anyfoo(): void}type A = RemoveIndexSignature<Foo> // expected { foo(): void }
起点
ts
/* _____________ Your Code Here _____________ */typeRemoveIndexSignature <T > = any/* _____________ Test Cases _____________ */typeFoo = {[key : string]: anyfoo (): void}typeBar = {[key : number]: anybar (): void0: string}constfoobar =Symbol ('foobar')typeFooBar = {[key : symbol]: any[foobar ](): void}typeBaz = {bar (): voidbaz : string}typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <RemoveIndexSignature <Foo >, {foo (): void }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <RemoveIndexSignature <Bar >, {bar (): void, 0: string }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <RemoveIndexSignature <FooBar >, { [foobar ](): void }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <RemoveIndexSignature <Baz >, {bar (): void,baz : string }>>,]
take the challengets
/* _____________ Your Code Here _____________ */typeRemoveIndexSignature <T > = any/* _____________ Test Cases _____________ */typeFoo = {[key : string]: anyfoo (): void}typeBar = {[key : number]: anybar (): void0: string}constfoobar =Symbol ('foobar')typeFooBar = {[key : symbol]: any[foobar ](): void}typeBaz = {bar (): voidbaz : string}typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <RemoveIndexSignature <Foo >, {foo (): void }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <RemoveIndexSignature <Bar >, {bar (): void, 0: string }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <RemoveIndexSignature <FooBar >, { [foobar ](): void }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <RemoveIndexSignature <Baz >, {bar (): void,baz : string }>>,]
解决方案
Spoiler warning // Click to reveal answer
ts
// most populartypeRemoveIndexSignature <T ,P =PropertyKey > = {[K in keyofT asP extendsK ? never :K extendsP ?K : never]:T [K ]}
ts
// most populartypeRemoveIndexSignature <T ,P =PropertyKey > = {[K in keyofT asP extendsK ? never :K extendsP ?K : never]:T [K ]}