Skip to content

[p5.js 2.0 Bug Report]: Typescript declarations missing overloads/optional parameters #7862

Open
@SoundOfScooting

Description

@SoundOfScooting

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.0.3

Web browser and version

No response

Operating system

No response

Steps to reproduce this

There are many missing overloads in the generated TypeScript definitions, most of which are functions with runtime optional parameters that are not declared as optional.
Usage of these missing overloads is an error when using "strict": true in tsconfig.json.

Some examples in p5.d.ts (not an exhaustive list):

// alpha should be optional
color(v1: number, v2: number, v3: number, alpha: number): p5.Color;
background(colorstring: string, a: number): void;
background(gray: number, a: number): void;
background(v1: number, v2: number, v3: number, a: number): void;
background(image: p5.Image, a: number): void;
clear(r: number, g: number, b: number, a: number): void;
/* correct */ fill(v1: number, v2: number, v3: number, alpha?: number): void;
fill(gray: number, alpha: number): void;
stroke(v1: number, v2: number, v3: number, alpha: number): void;
stroke(gray: number, alpha: number): void;

// options should be optional
beginClip(options: object): void;
clip(callback: Function, options: object): void;

// n should be optional
redraw(n: number): void;

// both params should be optional
erase(strengthFill: number, strengthStroke: number): void;

// these empty overloads are missing
print(): void;
fullscreen(): boolean;

// these fields are missing
width: number;
height: number;

// etc

Tested using p5 instance mode.

To actually use the generated types I had to change this part of p5/package.json.

".": "./dist/app.js",
// ->
".": {
  "types": "./types/p5.d.ts",
  "default": "./dist/app.js"
},

I'm compiling to an HTML file that can be opened offline with file://, but because of CORS the HTML file cannot use JS modules and I must disable the friendly error system.
To do this I've used Parcel to bundle the files with settings "outputFormat": "global", "scopeHoist": false, but it requires me to comment out a block if (typeof P5_DEV_BUILD !== 'undefined') { inside lib/p5.js (p5.min.js works fine).

This is my current working setup (suggestions welcome):

import type * as p5 from "p5";

// for some reason p5.default has different constructor signature than p5.p5
const P5 = (globalThis.p5 as any as typeof p5.default);

// this declaration is missing
(P5 as any).disableFriendlyErrors = true;

new P5(p5 => { /* ... */ });

Metadata

Metadata

Type

No type

Projects

Status

In Progress

Relationships

None yet

Development

No branches or pull requests

Issue actions