Skip to content

Fix/methods named new#127

Merged
MaxGraey merged 4 commits intoAssemblyScript:mainfrom
chharvey:fix/methods-named-new
Apr 28, 2026
Merged

Fix/methods named new#127
MaxGraey merged 4 commits intoAssemblyScript:mainfrom
chharvey:fix/methods-named-new

Conversation

@chharvey
Copy link
Copy Markdown
Contributor

@chharvey chharvey commented Apr 27, 2026

  1. fixes methods named new
    TypeScript treats new(...P): R; in an object type as a constructor signature.
    The caller is then expected to call the method like a constructor,
    e.g. new mod.struct(...args), which is incorrect.

    With that syntax, the method called correctly, mod.struct.new(...args), raises an error:

    Property 'new' does not exist on type 'T'.

    The fix is to make new a regular property with a function type.

  2. allows readonly array arguments
    Widens parameter types to include readonly arrays.

    Sometimes a caller may have a readonly array they want to pass
    into a function. If the function expects a writeable array,
    the assignment is not allowed:

    The type 'readonly T[]' is 'readonly' and cannot be assigned to the mutable type 'T[]'.

    That’s good typing, if the function actually mutates the array.
    But if it doesn’t, the parameter type is too narrow, and the caller is forced to use spread syntax, concating/slicing, type casting,
    or other ‘hacky’ techniques to bypass the error.

    By making the parameter type readonly T[],
    we’re promising the caller that the method will never mutate the array.
    I think that’s a reasonable assumption for all these methods.

  3. fixes signature of struct.new (parameter operands should be array of ExpressionRef)

  4. whitespace cleanup

TypeScript treats `new(...P): R;` in an object type as a constructor signature.
The caller is then expected to call the method like a constructor,
e.g. `new mod.struct(...args)`, which is incorrect.

With that syntax, the method called correctly, `mod.struct.new(...args)`, raises an error:
> Property 'new' does not exist on type 'T'.

The fix is to make `new` a regular property with a function signature.
Widens parameter types to include readonly arrays.

Sometimes a caller may have a readonly array they want to pass
into a function. If the function expects a writeable array,
the assignment is not allowed:
> The type 'readonly T[]' is 'readonly' and cannot be assigned to the mutable type 'T[]'.
This forces the caller to use spread syntax, concating/slicing, type casting,
or other ‘hacky’ techniques to resolve the error.

By making the parameter type `readonly T[]`,
we’re promising the caller that the method will never mutate the array.
@MaxGraey MaxGraey self-requested a review April 28, 2026 08:11
Copy link
Copy Markdown
Member

@MaxGraey MaxGraey left a comment

Choose a reason for hiding this comment

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

LGTM

Thanks!

@MaxGraey MaxGraey merged commit e42c00b into AssemblyScript:main Apr 28, 2026
2 checks passed
@chharvey chharvey deleted the fix/methods-named-new branch April 28, 2026 16:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants