microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
99.26k stars 12.31k forks source link

Please Allow return `void` of `set accessor` #59105

Open finscn opened 2 weeks ago

finscn commented 2 weeks ago

🔍 Search Terms

set accessor , return, void

✅ Viability Checklist

⭐ Suggestion

For the following case, TypeScript 5.5.3 throws "A 'set' accessor cannot have a return type annotation."

interface Foo {
  set foo (value: number): void
}

the 'void' is a special type , 'set' accessor should be allowed to return it. Because I think in TS "Every function should/could have a return value , if it don't, please return void ."

📃 Motivating Example

I think , in a ts project , there should be a Uniform code specification, like this : "All functions have return type , or All functions don't have return type, no exception" .

💻 Use Cases

make my project under a Uniform code specification.

MartinJohns commented 2 weeks ago

I think , in a ts project , there should be a Uniform code specification, like this :
"All functions have return type , or All functions don't have return type, no exception" .

Just change your specification to:

"All functions have return type (when allowed) , or All functions don't have return type, no exception" .

Having a return type annotation kinda implies that another type could be returned, but setters can't return anything. A return type annotation for something that can't return doesn't make much sense.

whzx5byb commented 2 weeks ago

Related: https://github.com/microsoft/TypeScript/issues/28317

finscn commented 2 weeks ago

I think , in a ts project , there should be a Uniform code specification, like this : "All functions have return type , or All functions don't have return type, no exception" .

Just change your specification to:

"All functions have return type (when allowed) , or All functions don't have return type, no exception" .

Having a return type annotation kinda implies that another type could be returned, but setters can't return anything. A return type annotation for something that can't return doesn't make much sense.

the " (when allowed)" is an exception . If a specification have some exceptions , it will lost control in a big team. the team leader have to spend time to explain what / when / why allowed and have to make team members understand the rules exactly ( in a big team , it's very hard , because Everyone is at a different level).

ESLint doesn't always help.


And , In fact , I can't understand why TS allows setFoo():v oid , but don't set foo() : void . In my opinion, void should be an optional , when a function/accessor does't return anything , with : void or without it , both ok.