-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpick.ts
More file actions
25 lines (20 loc) · 688 Bytes
/
pick.ts
File metadata and controls
25 lines (20 loc) · 688 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { internalRecord } from './record'
import { InternalRuntype, Runtype, RuntypeUsageError } from './runtype'
/**
* Build a new record runtype that contains some keys from the original
*/
export function pick<T, K extends keyof T>(
original: Runtype<T>,
...keys: K[]
): Runtype<Pick<T, K>> {
const fields = (original as InternalRuntype<any>).meta?.fields
const isNonStrict = (original as InternalRuntype<any>).meta?.isNonStrict
if (!fields) {
throw new RuntypeUsageError(`expected a record runtype`)
}
const newRecordFields: any = {}
keys.forEach((k: any) => {
newRecordFields[k] = fields[k]
})
return internalRecord(newRecordFields, isNonStrict)
}