Fix/methods named new#127
Merged
MaxGraey merged 4 commits intoAssemblyScript:mainfrom Apr 28, 2026
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fixes methods named
newTypeScript 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:The fix is to make
newa regular property with a function type.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:
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.
fixes signature of
struct.new(parameteroperandsshould be array of ExpressionRef)whitespace cleanup