πŸ›‘
Atomico with Typescript
Atomico with Typescript will improve the scalability of your project thanks to a really productive type system when creating, distributing and maintaining webcomponents
With Atomico and Typescript you will be able to:
πŸ“Œ If you are a Typescript user I recommend using TSX vs the template-string, as you will benefit from:
  1. 1.
    JSX runtime, Typescript will automatically import Atomico upon detecting the use of TSX.
  2. 2.
    Autocompletion and validation of attributes, properties and events of tags and webcomponents.
  3. 3.
    Validacion de instancia de webcomponents usando constructores.

Construct the props parameter of your functional component.

Your component as a function does not infer the props, to help infer the props you should use the Props type, example:
From an object: Construct props directly from the declaring object.
1
import { Props } from "atomico";
2
​
3
const props = {
4
value: { type: Number, value: 0 },
5
};
6
​
7
function myComponent({ value }: Props<typeof props>) {
8
return <host>{value * 10}</host>;
9
}
10
​
11
myComponent.props = props;
Copied!
From the component: Build the props from the component, internally Atomico captures the property component.props.
1
import { Props } from "atomico";
2
​
3
function myComponent({ value }: Props<typeof myComponent>) {
4
return <host>{value * 10}</host>;
5
}
6
​
7
myComponent.props = {
8
value: { type: Number, value: 0 },
9
};
Copied!
From another customElement component: It's unusal, but when you extend another component you can infer the props from its constructor.
1
import { Props, c } from "atomico";
2
import { MyOtherComponent } from "./my-other-component";
3
​
4
function myComponent({ value }: Props<typeof MyOtherComponent>) {
5
return <host>{value * 10}</host>;
6
}
7
​
8
export const MyComponent = c(myComponent, MyOtherComponent);
Copied!

Check the correct declaration of your component.

Typescript checks the structure of your component by using the c function on it, thus validating which props and styles have been attached.
1
import { Props, c, css } from "atomico";
2
​
3
function component(props: Props<typeof component>) {
4
return <host>{props.value}</host>;
5
}
6
​
7
component.props = {
8
value: Number,
9
};
10
​
11
component.styles = css`
12
:host {
13
color: tomato;
14
}
15
`;
16
​
17
const MyComponent = c(component);
18
​
19
customElements.define("my-component", MyComponent);
Copied!
The return of c is a CustomElement with the types of the associated props, example:
1
const myComponent = new MyComponent();
2
​
3
myComponent.value = "abc";
Copied!
It will report an error to Typescript since this component's props define that value is of type number, this validation also applies when instantiating your CustromElement in JSX or TSX.
1
<MyComponent value="abc"></MyComponent>
Copied!
Both TSX or JSX will help you to build better applications since it verifies the types according to the definition of its component, this verification is Free, it does not need plugins, only a tsconfig.json file and Typescript to automate the revision.