-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Revert typed SvelteComponent, add SvelteComponentTyped instead #5738
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,7 +97,57 @@ export function validate_slots(name, slot, keys) { | |
} | ||
} | ||
|
||
export interface SvelteComponentDev< | ||
type Props = Record<string, any>; | ||
export interface SvelteComponentDev { | ||
$set(props?: Props): void; | ||
$on(event: string, callback: (event: any) => void): () => void; | ||
$destroy(): void; | ||
[accessor: string]: any; | ||
} | ||
/** | ||
* Base class for Svelte components with some minor dev-enhancements. Used when dev=true. | ||
*/ | ||
export class SvelteComponentDev extends SvelteComponent { | ||
/** | ||
* @private | ||
* For type checking capabilities only. | ||
* Does not exist at runtime. | ||
* ### DO NOT USE! | ||
*/ | ||
$$prop_def: Props; | ||
|
||
constructor(options: { | ||
target: Element; | ||
anchor?: Element; | ||
props?: Props; | ||
hydrate?: boolean; | ||
intro?: boolean; | ||
$$inline?: boolean; | ||
}) { | ||
if (!options || (!options.target && !options.$$inline)) { | ||
throw new Error("'target' is a required option"); | ||
} | ||
|
||
super(); | ||
} | ||
|
||
$destroy() { | ||
super.$destroy(); | ||
this.$destroy = () => { | ||
console.warn('Component was already destroyed'); // eslint-disable-line no-console | ||
}; | ||
} | ||
|
||
$capture_state() {} | ||
|
||
$inject_state() {} | ||
} | ||
|
||
// TODO https://github.com/microsoft/TypeScript/issues/41770 is the reason | ||
// why we have to split out SvelteComponentTyped to not break existing usage of SvelteComponent. | ||
// Try to find a better way for Svelte 4.0. | ||
|
||
export interface SvelteComponentTyped< | ||
Props extends Record<string, any> = any, | ||
Events extends Record<string, any> = any, | ||
Slots extends Record<string, any> = any | ||
|
@@ -107,12 +157,42 @@ export interface SvelteComponentDev< | |
$destroy(): void; | ||
[accessor: string]: any; | ||
} | ||
|
||
export class SvelteComponentDev< | ||
/** | ||
* Base class to create strongly typed Svelte components. | ||
* This only exists for typing purposes and should be used in `.d.ts` files. | ||
* | ||
* ### Example: | ||
* | ||
* You have component library on npm called `component-library`, from which | ||
* you export a component called `MyComponent`. For Svelte+TypeScript users, | ||
* you want to provide typings. Therefore you create a `index.d.ts`: | ||
* ```ts | ||
* import { SvelteComponentTyped } from "svelte"; | ||
* export class MyComponent extends SvelteComponentTyped<{foo: string}> {} | ||
* ``` | ||
* Typing this makes it possible for IDEs like VS Code with the Svelte extension | ||
* to provide intellisense and to use the component like this in a Svelte file | ||
* with TypeScript: | ||
* ```svelte | ||
* <script lang="ts"> | ||
* import { MyComponent } from "component-library"; | ||
* </script> | ||
* <MyComponent foo={'bar'} /> | ||
* ``` | ||
* | ||
* #### Why not make this part of `SvelteComponent(Dev)`? | ||
* Because | ||
* ```ts | ||
* class ASubclassOfSvelteComponent extends SvelteComponent<{foo: string}> {} | ||
* const component: typeof SvelteComponent = ASubclassOfSvelteComponent; | ||
* ``` | ||
* will throw a type error, so we need to seperate the more strictly typed class. | ||
*/ | ||
export class SvelteComponentTyped< | ||
Props extends Record<string, any> = any, | ||
Events extends Record<string, any> = any, | ||
Slots extends Record<string, any> = any | ||
> extends SvelteComponent<Props, Events> { | ||
> extends SvelteComponentDev { | ||
/** | ||
* @private | ||
* For type checking capabilities only. | ||
|
@@ -142,24 +222,9 @@ export class SvelteComponentDev< | |
hydrate?: boolean; | ||
intro?: boolean; | ||
$$inline?: boolean; | ||
}) { | ||
if (!options || (!options.target && !options.$$inline)) { | ||
throw new Error("'target' is a required option"); | ||
} | ||
|
||
super(); | ||
}) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whitespace. |
||
super(options); | ||
} | ||
|
||
$destroy() { | ||
super.$destroy(); | ||
this.$destroy = () => { | ||
console.warn('Component was already destroyed'); // eslint-disable-line no-console | ||
}; | ||
} | ||
|
||
$capture_state() {} | ||
|
||
$inject_state() {} | ||
} | ||
|
||
export function loop_guard(timeout) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this broken to begin with? Otherwise this is a breaking change, no? The syntax
extends SvelteComponent<...> { ... }
ceases to work, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That syntax was introduced in
3.30
but reverted in3.31
because it introduced a breaking change.const a: typeof SvelteComponent = ASubclassOfSvelteComponent
threw errors which it did not before. So yes, the syntaxextends SvelteComponent<...> { ... }
ceases to work, but it only worked for a few days / in3.30.0
.