Skip to content

Conversation

@Gudine
Copy link
Contributor

@Gudine Gudine commented Sep 18, 2025

I've made the query functions (query, useQuery, useCurrentFile, etc.) accept a generic to specify its return type, much like the built-in querySelector type.

To make that more useful, I also added a way to specify the types of fields in classes with fields, useful in cases where you know what fields pages or sections returned by the query has. For example:

type PersonPage = MarkdownPage.Typed<{ name: string, age: number }>;

return function View() {
  const person = dc.useCurrentFile<PersonPage>();

  // Automatically typed as string
  const name = person.value("name");

  // Automatically typed as number
  const age = person.value("age");

  // Error: Argument of type '"height"' is not assignable to parameter of type '"name" | "age"'
  console.log(person.value("height"));

  return `${name}, ${age} years old`;
}

Add generic for indexable getters (query functions, as well as `currentFile` and similar) to specify the return type.
Add utility types, accessible in [className].Typed, that allows for field-related functions to give narrower results, if the user knows a file/section/etc. has specific fields.
@blacksmithgu blacksmithgu merged commit 234ab89 into blacksmithgu:master Sep 22, 2025
1 check passed
@blacksmithgu
Copy link
Owner

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants