Skip to content

Commit

Permalink
fix: type one way bindings that are not the element
Browse files Browse the repository at this point in the history
  • Loading branch information
dummdidumm committed Jun 26, 2023
1 parent 48ae8ce commit 0f2396e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/preprocessors/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ You may need to set `baseUrl` in `tsconfig.json` at the project root to include

### I'm using an attribute/event on a DOM element and it throws a type error

If it's a non-experimental standard attribute/event, this may very well be a missing typing from our [HTML typings](https://github.com/sveltejs/svelte/blob/master/elements/index.d.ts). In that case, you are welcome to open an issue and/or a PR fixing it.
If it's a non-experimental standard attribute/event, this may very well be a missing typing from our [HTML typings](https://github.com/sveltejs/svelte/blob/master/packages/svelte/elements.d.ts). In that case, you are welcome to open an issue and/or a PR fixing it.

In case this is a custom or experimental attribute/event, you can enhance the typings like this:
Create a `additional-svelte-typings.d.ts` file:
Expand Down
26 changes: 21 additions & 5 deletions packages/svelte2tsx/src/htmlxtojsx_v2/nodes/Binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { rangeWithTrailingPropertyAccess, TransformationArray } from '../utils/n
import { BaseDirective, BaseNode } from '../../interfaces';
import { Element } from './Element';
import { InlineComponent } from './InlineComponent';
import { surroundWithIgnoreComments } from '../../utils/ignore';

const oneWayBindingAttributes: Set<string> = new Set([
'clientWidth',
Expand All @@ -17,12 +18,16 @@ const oneWayBindingAttributes: Set<string> = new Set([
'ended',
'readyState',
'naturalWidth',
'naturalHeight',
'contentRect',
'contentBoxSize',
'borderBoxSize',
'devicePixelContentBoxSize'
'naturalHeight'
]);

const oneWayBindingAttributesNotOnElement: Map<string, string> = new Map([
['contentRect', 'DOMRectReadOnly'],
['contentBoxSize', 'ResizeObserverSize[]'],
['borderBoxSize', 'ResizeObserverSize[]'],
['devicePixelContentBoxSize', 'ResizeObserverSize[]']
]);

/**
* List of all binding names that are transformed to sth like `binding = variable`.
* This applies to readonly bindings and the this binding.
Expand Down Expand Up @@ -78,6 +83,17 @@ export function handleBinding(
return;
}

// one way binding whose property is not on the element
if (oneWayBindingAttributesNotOnElement.has(attr.name) && element instanceof Element) {
element.appendToStartEnd([
[attr.expression.start, attr.expression.end],
`= ${surroundWithIgnoreComments(
`null as ${oneWayBindingAttributesNotOnElement.get(attr.name)}`
)};`
]);
return;
}

// other bindings which are transformed to normal attributes/props
const isShorthand = attr.expression.start === attr.start + 'bind:'.length;
const name: TransformationArray =
Expand Down

0 comments on commit 0f2396e

Please sign in to comment.