Merge pull request #18475 from strapi/ts/replace-util

This commit is contained in:
Marc Roig 2023-10-25 16:23:34 +02:00 committed by GitHub
commit ea26b0dc3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 0 deletions

View File

@ -30,6 +30,9 @@ type Values = Utils.Object.Values<{ foo: 'bar'; bar: 'foo'; foobar: 2 }>;
type ValuesNever = Utils.Object.Values<never>;
type ValuesContainNever = Utils.Object.Values<{ foo: 'bar'; bar: 'foo'; foobar: never }>;
// Replace
type Replace = Utils.Object.Replace<{ foo: 'bar'; bar: 'foo' }, { foo: 2 }>;
export {
// KeysBy
KeysByString,
@ -56,4 +59,7 @@ export {
Values,
ValuesNever,
ValuesContainNever,
// Replace
Replace,
};

View File

@ -77,4 +77,15 @@ describe('Utils.Object', () => {
type('ValuesNever').isNever();
type('ValuesContainNever').isUnion([t.stringLiteral('foo'), t.stringLiteral('bar')]);
});
test.skip('Replace', () => {
// const expectedResultType = t.object({
// properties: {
// foo: t.numberLiteral(2),
// bar: t.stringLiteral('foo'),
// },
// });
// // TODO: Fix object type check
// type('Replace').isObject(expectedResultType);
});
});

View File

@ -64,3 +64,15 @@ export type DeepPartial<TObject> = TObject extends object
[TKey in keyof TObject]?: DeepPartial<TObject[TKey]>;
}
: TObject;
/**
* Replace the keys of an object with the keys of another object
*
* @example
* type X = Replace<{ foo: number, bar: number}, { foo: string }>
* // { foo: string, bar: number }
*/
export type Replace<
TObject extends object,
TNew extends Partial<{ [key in keyof TObject]: unknown }>
> = Omit<TObject, keyof TNew> & TNew;