Index of every page on this site: llms.txt
TypeScript
Schema

Question and Answer Schema API with JSON-LD

Implementation overview

Use defineQuestion() to connect a question with its accepted answer in JSON-LD. This reference covers required values, generated identifiers, WebPage relationships, aliases, and complete TypeScript examples.

Schema.org Question

  • Type: defineQuestion<T extends Record<string, any>>(input?: Question & T)
    Describes an individual question. It is commonly used on an FAQPage.

Question and answer properties

  • name string or question string
    The text content of the question.
  • acceptedAnswer string | Answer or answer string
    The text content of the answer.

Examples

Minimal

defineQuestion({
  name: 'What is the meaning of life?',
  acceptedAnswer: '42',
})

Defaults

  • @type: Question
  • @id: ${canonicalUrl}#/schema/question/{n}
  • inLanguage: inLanguage from resolved page metadata (see: Schema.org Params)

Resolves

See Global Resolves for full context.

  • converts the question alias to name and the answer alias to acceptedAnswer
  • converts a string answer to an Answer object
  • assigns numbered IDs in registration order when @id is not provided
  • serializes dateCreated Date objects as ISO 8601 strings

Relation Transforms

WebPage

  • Each question appends an entry to mainEntity when the WebPage includes the FAQPage type

Types

/**
 * A specific question, e.g., from a user seeking answers online or collected in a Frequently Asked Questions (FAQ) document.
 */
export interface QuestionSimple extends Thing {
  /**
   * The text content of the question.
   */
  name?: string
  /**
   * An answer object, with a text property which contains the answer to the question.
   */
  acceptedAnswer?: NodeRelation<Answer | string>
  /**
   * The language code for the question; e.g., en-GB.
   */
  inLanguage?: string
  /**
   * The number of answers provided for this question.
   */
  answerCount?: number
  /**
   * The date and time the question was created.
   */
  dateCreated?: ResolvableDate
  /**
   * Alias for `name`
   */
  question?: string
  /**
   * Alias for `acceptedAnswer`
   */
  answer?: string
}

/**
 * An answer offered to a question; perhaps correct, perhaps opinionated or wrong.
 */
export interface AnswerSimple extends Thing {
  text: string
}

export interface Answer extends AnswerSimple {}
Did this page help you?