If a file defines some types like:
// lib.ts
type MySecretComplexType = 'SECRET' | false | 42 | true[]
export type MyRecordSet = Record<string, MySecretComplexType>
export function apiCall(): MyRecordSet { return { 'foo': false, 'bar': false }}
Is it possible for another file to extract the types from the MyRecordSet
? For example:
// myWorker.ts
import { MyRecordSet } from 'lib'
/**
* Worker function to act upon child values from a MyRecordSet
*/
function myWorker(inputThing: any) { // <==== How to explicitly set `inputThing`'s type?
console.log(typeof inputThing)
}
import { MyRecordSet, apiCall } from 'lib'
import { myWorker } from 'myWorker'
const result: MyRecordSet = apiCall()
for (const [key, value] of Object.entries(result)) {
console.log(`Working on ${key}...`)
myWorker(value)
}
How can the function myWorker
define the types of its inputs to “the type of the values of the Record type defined as MyRecordSet” (when MySecretComplexType
is not exported from the original library file)? Specifically where myWorker
is being defined in a separate file/module, so doesn’t have a result
object to inspect the type of, and can only go off the exported MyRecordSet
type?