tc39 / proposal-type-annotations

ECMAScript proposal for type syntax that is erased - Stage 1
https://tc39.es/proposal-type-annotations/
4.13k stars 44 forks source link

[QUESTION] Will/should this proposal affect the `.toString()` method of functions? #181

Closed spenserblack closed 1 year ago

spenserblack commented 1 year ago

Right now, .toString() returns a string representation of the function's source code:

function add(a, b) { return a + b /* comment */ }
console.log(add.toString()) // function add(a, b) { return a + b /* comment */ }

Python, which is mentioned a few times in this proposal and its issues, has this behavior:

def add(a: int, b: int) -> int: return a + b
help(add)  # add(a: int, b: int) -> int

I understand that Python's types are very different from this proposal's type annotations, and that Python's help is not the same as Function.prototype.toString, but this made me wonder if "a string representation of the function's source code" would include the type annotations, and result in something like this:

function add(a: number, b: number): number { return a + b }
console.log(add.toString()) // function add(a: number, b: number): number { return a + b }
ljharb commented 1 year ago

There's existing rules for whether comments are included are not, and type annotations would just be another form of comment, so I assume it'd follow those same rules and include the examples indicated.

benjamingr commented 1 year ago

Like Jordan said:

function foo(a /*number*/) {}
foo.toString(); // 'function foo(a /*number*/) {}'
spenserblack commented 1 year ago

Thanks for the prompt answers! I'll close this as "answered."