All functions, by default, have a
Telling TypeScript not to allow
this to be used will eliminate that parameter from its containing context.
Other workarounds are available for removing this too.
Disabling this self parameter should mainly be done when writing declarations for Lua interop purposes.
This is a type-safe solution usable whenever describing something callable.
This tells TypeScript that
this cannot be used in the context of this function.
this: void results in no
self parameter to be generated.
Also useful if you have class methods which should be called with a dot
. instead of a colon
Common Lua libraries use callback functions that don't have a
self parameter so make sure this is reflected in their declaration.
If you wish to specify that all functions in a class, interface or namespace should not have a context parameter, you can use the
You can override
@noSelf on a per-function basis by specifying a
If you want to specify that all functions in a file should have no context, you can use
@noSelfInFile at the top of the file.
Use this option if you do not want implemented functions to have a self parameter.
Ambient functions (functions described, not implemented) as well as classes and interfaces ignore this option.
Use this with
noImplicitThis to ensure you don't use an "implicit this" type in your code.
When enabled, if
this has a type other than an implicit
self parameter will be added for its containing function.
See the two types below.
NoContext to be assignable to
TypeScriptToLua does not.
❌ Error: Unable to convert function with a 'this' parameter to function with no 'this'. To fix, wrap in an arrow function, or declare with 'this: void'.
This throws an error because
callback's type is not assignable to
cb's type since the latter has an implicit any type which changes how the function should be called.
To fix this, an arrow function can be used.
TypeScript says the arrow function has no context due to the parameter's signature making TypeScriptToLua accept the parameter.
A similar error occurs if a function is overloaded and the call signature differs between how to use context:
❌ Error: Unsupported assignment of function with different overloaded types for 'this'. Overloads should all have the same type for 'this'.
It's best practice to avoid overloads with different context types.