You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TypeScript 3.9.2, compiler options noImplicitThis, noImplicitAny, strict, allwaysStrict
type checking
Suggestion
Currently, a class method without a this parameter can be assigned to a variable of a function type that has a this: void parameter. This should not be possible if compiler option noImplicitThis is set to true, because when the variable with function type is called this can be equal to undefined and the class method body can have calls like this.name = value which causes a runtime error (see Example 1 below).
Furthermore, a function with this: any parameter can currently be assigned to a variable with the same type but a this: void parameter. Calling that variable once again can cause a runtime error because of the same reason as above. This should not be possible even with noImplicitThis set to false (see Example 2 below).
I tested these cases with VS Code 1.45, TypeScript 3.9.2, and compiler options noImplicitThis, noImplicitAny, strict, and allwaysStrict set to true (beside other options).
noImplicitThis in the current implementation is more a kind of noImplicitAnyThis: Functions without a this parameter, and which are not methods, cannot use this in their body (this would be implicitly any), but methods without a this parameter can use this in their body, and their method signature does not even implicitly get a this parameter assigned by the compiler or language server (this would be implicitly assigned the type of the class).
Please change the behaviour of compiler option noImplicitThis or add another compiler option strictThis which implies the above mentioned stricter this type checks.
interfaceUIElement{addClickListener(onclick: (this: void,e: ErrorEvent)=>void): void;}classSphereimplementsUIElement{addClickListener(onclick: (this: void,e: ErrorEvent)=>void): void{// Raises "TypeError: Cannot set property 'info' of undefined"onclick(newErrorEvent('Event Error'));}}classHandler{info: string|undefined;onClickBad(e: ErrorEvent){// oops, used `this` here. using this callback would crash at runtimethis.info=e.message;};}leth=newHandler();constuiElement: UIElement=newSphere();// the following does not cause a compile time error but a run time error!uiElement.addClickListener(h.onClickBad);
Example 2
letfuncAny=function(this: any){this.name='Microsoft';};letfuncVoid : (this: void)=>any=funcAny;// does not cause a compile time error!funcVoid();// causes a run time error!
Checklist
My suggestion meets these guidelines:
This wouldn't be a breaking change in existing TypeScript/JavaScript code
This wouldn't change the runtime behavior of existing JavaScript code
This could be implemented without emitting different JS based on the types of the expressions
This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
The text was updated successfully, but these errors were encountered:
PetaSoft
changed the title
Compiler option "noImplcitThis" should imply more strict type checks
Compiler option "noImplictThis" should imply more strict type checks
Jun 8, 2020
PetaSoft
changed the title
Compiler option "noImplictThis" should imply more strict type checks
Compiler option "noImplictThis" should imply stricter type checks
Jun 8, 2020
Search Terms
TypeScript 3.9.2, compiler options
noImplicitThis
,noImplicitAny
,strict
,allwaysStrict
type checking
Suggestion
Currently, a class method without a
this
parameter can be assigned to a variable of a function type that has athis: void
parameter. This should not be possible if compiler optionnoImplicitThis
is set totrue
, because when the variable with function type is calledthis
can be equal toundefined
and the class method body can have calls likethis.name = value
which causes a runtime error (see Example 1 below).Furthermore, a function with
this: any
parameter can currently be assigned to a variable with the same type but athis: void
parameter. Calling that variable once again can cause a runtime error because of the same reason as above. This should not be possible even withnoImplicitThis
set tofalse
(see Example 2 below).I tested these cases with VS Code 1.45, TypeScript 3.9.2, and compiler options
noImplicitThis
,noImplicitAny
,strict
, andallwaysStrict
set totrue
(beside other options).noImplicitThis
in the current implementation is more a kind ofnoImplicitAnyThis
: Functions without athis
parameter, and which are not methods, cannot usethis
in their body (this
would be implicitlyany
), but methods without athis
parameter can usethis
in their body, and their method signature does not even implicitly get athis
parameter assigned by the compiler or language server (this
would be implicitly assigned the type of theclass
).Please change the behaviour of compiler option
noImplicitThis
or add another compiler optionstrictThis
which implies the above mentioned stricterthis
type checks.Examples
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: