2021-12-14
Proposal: validate type of object properties · Issue #141 · sindresorhus/is (github.com) #issues
- mscharley/generic-type-guard: Type safe, composable type guards for TypeScript (github.com) 스타일로 해결하는 방법 제안하기
- Object shape validator · Issue #92 · sindresorhus/ow (github.com)
ow
에는exactShape
와partialShape
가 있다.
tsimport is from '@sindresorhus/is'; import {objectEntries, objectHasOwn} from 'ts-extras'; const isInterface = <ObjectType extends Record<string, unknown>>( value: unknown, interface_: { [Key in keyof ObjectType]: (value: unknown) => value is ObjectType[Key]; }, ): value is ObjectType => { return objectEntries(interface_).every( ([key, predicate]) => objectHasOwn(value, key) && predicate(value[key]), ); }; declare const someObject: unknown; if ( isInterface(someObject, { foo: is.string, bar: is.number, baz: is.boolean, }) ) { someObject; // { // foo: string; // bar: number; // baz: boolean; // } }
import is from '@sindresorhus/is'; import {objectEntries, objectHasOwn} from 'ts-extras'; const isInterface = <ObjectType extends Record<string, unknown>>( value: unknown, interface_: { [Key in keyof ObjectType]: (value: unknown) => value is ObjectType[Key]; }, ): value is ObjectType => { return objectEntries(interface_).every( ([key, predicate]) => objectHasOwn(value, key) && predicate(value[key]), ); }; declare const someObject: unknown; if ( isInterface(someObject, { foo: is.string, bar: is.number, baz: is.boolean, }) ) { someObject; // { // foo: string; // bar: number; // baz: boolean; // } }
Vue
- vue-class-component에서
get
은computed
속성으로 동작한다. computed
속성은 반응형 종속성에 따라 캐싱된 결과를 리턴한다.