6

I'm working on an API (this is my first time in TypeScript). Traditionally you would type the response you would expect in the response.body of your API, something like this:

type ApiProps = {
  name: string
  email: string
}

Now if I want to know that response.body is an ApiProps object, from all the posts. I have found you have to write a type guard manually, doing all the checks. Isn't there a way to avoid writing manually all those type checks? something like:

function hasValidProps<T>(body: unknown): body is T {
  // some code using the type definition to validate the type "automatically"
}

Edit: Apparently in 2022 this is still not doable, but I did find a decent workaround below

function hasValidProps<T extends Record<string, unknown> = Record<string, unknown>>(
  body: unknown,
  props: T
): body is T {
  try {
    const bodyObject = JSON.parse(body as string) as Record<string, unknown>
    const bodyObjectKeys = Object.keys(bodyObject)
    const isValid = Object.keys(props).every((key) => bodyObjectKeys.includes(key))
    if (isValid) {
      body = bodyObject
    }
    return isValid
  } catch {
    return false
  }
}

const apiProps = {
  name: '',
  email: ''
}

export type ApiProps = typeof apiProps

// And in your API check...
if (!hasValidProps<ApiProps>(request.body, apiProps)) {
    response.status(400).end()
    return
}
8
  • You can use JSON Schema and Ajv JSON Schema validator. You will however need to define JSON Schemas e.g. for your API but that's great documentation and anyone using your API would exactly know what to send to your API. Commented Sep 28, 2022 at 19:46
  • This is exactly what I am trying to avoid. I don't want to write type guard but rather infer the type's properties at runtime. Commented Sep 28, 2022 at 19:46
  • I saw Ajv but it doesn't look like you can actually use the schemas as type - I was looking for something native to TypeScript ideally Commented Sep 28, 2022 at 19:49
  • The type system is gone at runtime. So if you can't know the possible types at compile time, then you can't know the possible types. Commented Sep 28, 2022 at 19:50
  • @NicolasBouvrette - There is no such thing native to TypeScript Commented Sep 28, 2022 at 19:50

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.