Skip to content

[p5.js 2.0 Bug Report]: Many chainable p5 instance methods are typed to return void rather than p5 #8415

@gwhitney

Description

@gwhitney

Most appropriate sub-area of p5.js?

  • Accessibility
  • Color
  • Core/Environment/Rendering
  • Data
  • DOM
  • Events
  • Image
  • IO
  • Math
  • Typography
  • Utilities
  • WebGL
  • Build process
  • Unit testing
  • Internationalization
  • Friendly errors
  • Other (specify if possible)

p5.js version

v2.2.0-rc6

Web browser and version

n/a

Operating system

OpenSUSE Tumbleweed Linux (up-to-date)

Steps to reproduce this

Steps:

  1. In a TypeScript instance-mode project, in which the variable sketch has type p5, write sketch.background(this.backgroundColor).strokeWeight(0).
  2. Attempt to compile with tsc
  3. Obtain the error
TS2339: Property 'strokeWeight' does not exist on type 'void'.

663         sketch.background(this.backgroundColor).strokeWeight(0)

(this happened to occur on line 663 of my code, which is not relevant).

Discussion:

The error is unsurprising, in that the p5.d.ts file generated by npm run generate-types contains the following declarations:

  background(color: p5.Color): void;
  background(colorstring: string, a?: number): void;
  background(gray: number, a?: number): void;
  background(v1: number, v2: number, v3: number, a?: number): void;
  background(values: number[]): void;
  background(image: p5.Image, a?: number): void;

But these declarations are incorrect because the implementation of background() does not return void:

fn.background = function(...args) {
      this._renderer.background(...args);
      return this;
    };

And indeed, the JSDoc comments for all of the signatures of background explicitly specify @chainable, meaning they return the object they were called on. So this appears to be a bug in generate-types; all of those void return types for background should be p5. And note this is not just on background; there are multiple other chainable methods on a p5 instance that are declared to return void, including at least (but probably not limited to, these are just the ones creating errors in our code and making it impossible to upgrade to p5.js 2.1+):

stroke, fill, textSize (with an argument), noStroke, frameRate (with an argument), translate.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions