Closed saylerb closed 4 years ago
Why does your function has a this
argument?
@PatrickG that is the convention for specifying the type of this in TypeScript, as described in the Sapper Docs. https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#specifying-the-type-of-this-for-functions
No, that would be
function (this: Type) {}
You did not specify any Type
.
Edit: I see it is like this in the example. Looks strange.
I believe since I’m providing the return type, TypeScript can infer the type. Are you saying these docs are wrong? https://sapper.svelte.dev/docs#Typing_the_function
The example should work, whether you specify this
as an argument or not (although you do not need to specify it).
I just tested it with a fresh install.
It works if you remove the empty declare module
lines in src/node_modules/@sapper/index.d.ts
. There is already a PR that does this (https://github.com/sveltejs/sapper/pull/1598), but it is not released yet.
Closing this since the fix has already been checked in. Thanks @PatrickG for helping with this!
The example should work, whether you specify
this
as an argument or not (although you do not need to specify it).I just tested it with a fresh install. It works if you remove the empty
declare module
lines insrc/node_modules/@sapper/index.d.ts
. There is already a PR that does this (sveltejs/sapper#1598), but it is not released yet.
Ah okay. Thanks for the heads up. I'll be looking out for that next release, I guess.
Not sure if this a mistake in the Sapper documentation, or an issue with the template, or something with how TypeScript is configured. I'm new to TypeScript, so apologies if there's something super obvious that I'm missing here.
Using a fresh install of the sapper template on node 14.15.0 (fermium) and running the TypeScript conversion script, I wanted to test out using the new types for the
preload
function using the code example in the docs.However, using the example from the docs verbatim to add types to the preload in
blog/index.svelte
like this:I get the TypeScript compilation error:
Changing the type import to
import type Common from @sapper/common
fixes the compilation error, and the type ofthis
is correctly inferred to beCommon.PreloadContext
:Is the description in the docs wrong? Or am I missing something about TypeScript, where the template's configuration might be wrong? Here's a link to the test repo I made with both the broken and fixed version as two different commits.