anysphere / priompt

Prompt design using JSX.
MIT License
849 stars 51 forks source link

Make `PromptNode` and `PromptElement` types more React-like #4

Closed unknown closed 5 months ago

unknown commented 8 months ago

This PR makes PromptNode and PromptElement types more similar to React.ReactNode and React.ReactElement, respectively. That is, the PromptElement type now encapsulates all the internal object representations of elements created through Prompt.createElement and JSX transpilation, and PromptNode now encapsulates all types that can be rendered (the PromptElement type in addition to literal types like strings and numbers).

Fixing these types allows some Array.prototype.flat() calls that were used to wrangle with Typescript to be removed.

NOTE: This change may require changing the return types of Priompt components from PromptElement to PromptNode. For example, the following would cause a type error because the PromptElement type does not include strings, even though ExamplePrompt is a renderable component.

function ExamplePrompt(): PromptElement {
  return "Hello!";
}

Changing the PromptElement type to PromptNode would fix this type error.

arvid220u commented 8 months ago

looks good otherwise i think!

in general a bit worried about refactors since every single part of our production code relies on priompt, and even well-intentioned and well-thought-through refactors have a risk of introducing bugs (sometimes just because the previous implementation was buggy and other code unintentionally relied on that specific bugginess!)

unknown commented 8 months ago

looks good otherwise i think!

in general a bit worried about refactors since every single part of our production code relies on priompt, and even well-intentioned and well-thought-through refactors have a risk of introducing bugs (sometimes just because the previous implementation was buggy and other code unintentionally relied on that specific bugginess!)

Understand this completely! I've tried to keep the changes as minimal as possible and focused on just making Priompt's element types more similar to React's. There aren't any significant logic changes other than removing .flat() from some children props and the bugs in computePriorityLevels, validateNotBothAbsoluteAndRelativePriority, and validateNoChildrenHigherPriorityThanParent you highlighted.