Imports & Exports
Imports should appear at the top of the module as at some point it will be enforced by the compiler (to allow for simpler parallelized compilation).
There is no explicit syntax for exporting; all declarations can be imported from other modules by name.
Named Imports
Declarations can be imported from other modules by name.
- Knot
- TypeScript
utils.knknot
func myFunc -> "Hello";view MyView(content: string) -> <div>{content}</div>;
utils.knknot
func myFunc -> "Hello";view MyView(content: string) -> <div>{content}</div>;
core.knknot
import { myFunc, MyView } from "@/utils";const element = <MyView content=myFunc() />;
core.knknot
import { myFunc, MyView } from "@/utils";const element = <MyView content=myFunc() />;
utils.tsxtsx
function myFunc() {return "Hello";}function MyView(content: string) {return <div>{content}</div>;}
utils.tsxtsx
function myFunc() {return "Hello";}function MyView(content: string) {return <div>{content}</div>;}
core.tsxtsx
import { myFunc, MyView } from "./utils";const element = <MyView content={myFunc()} />;
core.tsxtsx
import { myFunc, MyView } from "./utils";const element = <MyView content={myFunc()} />;
Import Aliases
Sometimes you may end up with naming conflicts with other declarations if you import using the exact name something was exported with. Aliases allow you to rename it within the scope of the module to make it easier to recognize.
- Knot
- TypeScript
utils.knknot
func handler -> {};
utils.knknot
func handler -> {};
core.knknot
import { handler as fooHandler } from "@/utils";
core.knknot
import { handler as fooHandler } from "@/utils";
utils.tsxtsx
function handler() {}
utils.tsxtsx
function handler() {}
core.tsxtsx
import { handler as fooHandler } from "./utils";
core.tsxtsx
import { handler as fooHandler } from "./utils";
Main Exports
You can also import the main export of any module which is denoted with the main
keyword.
The import pattern allows importing the just the main
export, just named exports, or a combination of the two.
- Knot
- TypeScript
view.knknot
main view MainView -> <div>Welcome!</div>;
view.knknot
main view MainView -> <div>Welcome!</div>;
constants.knknot
main const otherConst = 100;
constants.knknot
main const otherConst = 100;
core.knknot
import MainView from "@/views";import mainConst, { otherConst } from "@/constants";const truthy = mainConst == otherConst;
core.knknot
import MainView from "@/views";import mainConst, { otherConst } from "@/constants";const truthy = mainConst == otherConst;
view.tsxtsx
export default function MainView() {return <div>Welcome!</div>;}
view.tsxtsx
export default function MainView() {return <div>Welcome!</div>;}
constants.tstsx
export const otherConst = 100;export default otherConst;
constants.tstsx
export const otherConst = 100;export default otherConst;
core.tsxtsx
import MainView from "./views";import mainConst, { otherConst } from "./constants";const truthy = mainConst == otherConst;
core.tsxtsx
import MainView from "./views";import mainConst, { otherConst } from "./constants";const truthy = mainConst == otherConst;