MOBILE-4059 core: Improve CoreObject types
Type helper extracted from https://stackoverflow.com/questions/57571664/typescript-type-for-an-object-with-only-one-key-no-union-type-allowed-as-a-key/57576688#57576688main
parent
608ea978a0
commit
9f26620e03
|
@ -12,13 +12,18 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
export type CoreObjectWithoutEmpty<T> = {
|
||||
[k in keyof T]: T[k] extends undefined | null ? never : T[k];
|
||||
};
|
||||
import { Pretty } from '@/core/utils/types';
|
||||
|
||||
export type CoreObjectWithoutUndefined<T> = {
|
||||
[k in keyof T]: T[k] extends undefined ? never : T[k];
|
||||
};
|
||||
type ValueWithoutEmpty<T> = T extends null | undefined ? never : T;
|
||||
type ValueWithoutUndefined<T> = T extends undefined ? never : T;
|
||||
|
||||
export type CoreObjectWithoutEmpty<T> = Pretty<{
|
||||
[k in keyof T]: ValueWithoutEmpty<T[k]>;
|
||||
}>;
|
||||
|
||||
export type CoreObjectWithoutUndefined<T> = Pretty<{
|
||||
[k in keyof T]: ValueWithoutUndefined<T[k]>;
|
||||
}>;
|
||||
|
||||
/**
|
||||
* Singleton with helper functions for objects.
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
// (C) Copyright 2015 Moodle Pty Ltd.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
/**
|
||||
* Helper type to flatten complex types.
|
||||
*/
|
||||
export type Pretty<T> = T extends infer U ? {[K in keyof U]: U[K]} : never;
|
Loading…
Reference in New Issue