-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
set but no get on class... but using get on instance of class compiles. #30852
Comments
TypeScript doesn't have a concept of |
Ok, so let's say I've implicitly created a getter. What is the return type of this getter? I would think it would have to be |
Also, I'm not sure the duplicate label fits. I'm not advocating that this usage pattern should be supported. I'm advocating that it be a compile error. The impact of emitting this code, for our application, was that we had a value of undefined assigned to a variable of type string. This defeating of the type system lead to unexpected crashes for us. I believe CSA has enough information to flag this assignment as illegal. I think it should. |
What is "it" ? The existence of a setter without a getter, or an assignment to the property? The former would be a breaking change, the latter needs representation in the type system, which is effectively the same as having a |
Sorry, by "it" I mean the assignment to the field
I think that assignment should generate a static error. I don't see the immediate connection to the
Our of curiosity, currently, what is the type assigned to the rhs expression of the assignment in the example code? |
To the absolute highest extent possible, an implementation file and a generated declaration file need to behave identically. Knowing that, take this class // a.ts
class A {
set y(s: string) {
}
} And run it through // a.d.ts
declare class A {
y: string;
} Note that it would be wrong to generate y: string | undefined; because the class doesn't allow writes of The correct declaration file would say writeonly y: string; |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Seems worth considering. With class Foo {
set prop(x: string) { }
// ~~~~ <--
// Property 'prop' has no 'get' accessor and its 'set' accessor type does not include 'undefined'.
// Either
} which could be silenced either by accepting class Bar {
set prop(x: string | undefined) { } // no error
} or by class Baz {
set prop(x: string) { } // no error
declare get prop(): string;
} Of course this is a breaking change, but is setter-only code more likely to be intentional or a mistake? In any case, people coming here should probably be aware of ESLint's accessor-pairs linter rule, since it might help them. |
TypeScript Version: 3.3.3333
Search Terms:
typescript default get when only set present
Expected behavior:
Error: no 'nextToInterpret' property on class 'CodeWorkflowState'
OR
Error: default compiler supplied accessor 'nextToInterpret' has type undefined but assigning to type string.
Actual behavior:
Compiles fine. Variable of type string has value undefined at runtime.
Playground Link: https://www.typescriptlang.org/play/index.html#src=namespace%20Controller%0D%0A%7B%0D%0A%20%20%20%20export%20class%20MainController%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%2F**%0D%0A%20%20%20%20%20%20%20%20%20*%20Tracks%20the%20current%20state%20of%20code%20moving%20from%20editor%2C%20to%20interpreter%2C%20to%20renderer.%0D%0A%20%20%20%20%20%20%20%20%20*%2F%0D%0A%20%20%20%20%20%20%20%20private%20readonly%20_codeWorkflowState%20%3D%20new%20CodeWorkflowState()%3B%0D%0A%0D%0A%20%20%20%20%20%20%20%20private%20handleInterpreterWorkerMessage(e%3A%20any)%20%2F%2F%20any%20because%20from%20outside%20TypeScript%20so%20can't%20enforce%20type%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20if(this._codeWorkflowState.hasNexToInterpret)%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20bug%3F%20nextToInterpret%20accessor%20does%20not%20exist.%20No%20static%20error%20reported.%20newerCode's%20value%20is%20undefined%20at%20runtime.%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20const%20newerCode%3A%20string%20%3D%20this._codeWorkflowState.nextToInterpret%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20class%20CodeWorkflowState%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20private%20_nextToInterpret%20%3A%20string%20%7C%20null%20%3D%20null%3B%0D%0A%0D%0A%20%20%20%20%20%20%20%20set%20nextToInterpret(code%3A%20string)%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20this._nextToInterpret%20%3D%20code%3B%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20%20%20%20%20get%20hasNexToInterpret()%20%3A%20boolean%0D%0A%20%20%20%20%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20this._nextToInterpret%20!%3D%3D%20null%3B%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%7D%0D%0A%7D
The text was updated successfully, but these errors were encountered: