Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript declaration files script #7465

Open
wants to merge 8 commits into
base: dev-2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/color/creating_reading.js
Original file line number Diff line number Diff line change
Expand Up @@ -1540,8 +1540,8 @@ function creatingReading(p5, fn){
* <a href="#/colorMode">colorMode()</a>.
*
* @method paletteLerp
* @param {[p5.Color|String|Number|Number[], Number][]} colors_stops color stops to interpolate from
* @param {Number} amt number to use to interpolate relative to color stops
* @param {Array.<Array.<(p5.Color|String|Number|Number[]), Number>>} colors_stops color stops to interpolate from
* @param {Number} amt number to use to interpolate relative to color stops
* @return {p5.Color} interpolated color.
*
* @example
Expand Down
50 changes: 49 additions & 1 deletion utils/generate-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,52 @@ function generateTypeFromTag(param) {
return normalizeTypeName(param.type.name);
case 'TypeApplication':
const baseType = normalizeTypeName(param.type.expression.name);

// Handle array cases
if (baseType === 'Array') {
const innerType = param.type.applications[0];

// Handle nested array that represents a tuple
if (innerType.type === 'TypeApplication' &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we able to handle this recursively instead of nesting like this? e.g. something like:

return `(${generateTypeFromTag(innerType)})[]`

innerType.expression.name === 'Array') {
// Get all tuple element types
const tupleTypes = innerType.applications
.map(app => generateTypeFromTag({ type: app }))
.join(', ');
return `Array<[${tupleTypes}]>`;
}

// Handle array with union type
if (innerType.type === 'UnionType') {
const unionTypes = innerType.elements
.map(el => generateTypeFromTag({ type: el }))
.join(' | ');
// If this is part of a tuple structure (has sibling types), wrap in tuple
if (param.type.applications.length > 1) {
const remainingTypes = param.type.applications
.slice(1)
.map(app => generateTypeFromTag({ type: app }))
.join(', ');
return `Array<[${unionTypes}, ${remainingTypes}]>`;
}
return `Array<${unionTypes}>`;
}

// Regular array
const typeParam = generateTypeFromTag({ type: innerType });
return `Array<${typeParam}>`;
}

// Regular type application
const typeParams = param.type.applications
.map(app => generateTypeFromTag({ type: app }))
.join(', ');
return `${baseType}<${typeParams}>`;
case 'UnionType':
return param.type.elements
const unionTypes = param.type.elements
.map(el => generateTypeFromTag({ type: el }))
.join(' | ');
return unionTypes;
case 'OptionalType':
return generateTypeFromTag({ type: param.type.expression });
case 'AllLiteral':
Expand All @@ -230,6 +268,16 @@ function generateTypeFromTag(param) {
return `'${param.type.value}'`;
case 'UndefinedLiteralType':
return 'undefined';
case 'ArrayType':
// Check if it's a tuple type (array with specific types for each position)
if (param.type.elements) {
const tupleTypes = param.type.elements
.map(el => generateTypeFromTag({ type: el }))
.join(', ');
return `[${tupleTypes}]`;
}
// Regular array type
return `${generateTypeFromTag({ type: param.type.elementType })}[]`;
default:
return 'any';
}
Expand Down