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.knknotfunc myFunc -> "Hello";view MyView(content: string) -> <div>{content}</div>;
utils.knknotfunc myFunc -> "Hello";view MyView(content: string) -> <div>{content}</div>;
core.knknotimport { myFunc, MyView } from "@/utils";const element = <MyView content=myFunc() />;
core.knknotimport { myFunc, MyView } from "@/utils";const element = <MyView content=myFunc() />;
utils.tsxtsxfunction myFunc() {return "Hello";}function MyView(content: string) {return <div>{content}</div>;}
utils.tsxtsxfunction myFunc() {return "Hello";}function MyView(content: string) {return <div>{content}</div>;}
core.tsxtsximport { myFunc, MyView } from "./utils";const element = <MyView content={myFunc()} />;
core.tsxtsximport { 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.knknotfunc handler -> {};
utils.knknotfunc handler -> {};
core.knknotimport { handler as fooHandler } from "@/utils";
core.knknotimport { handler as fooHandler } from "@/utils";
utils.tsxtsxfunction handler() {}
utils.tsxtsxfunction handler() {}
core.tsxtsximport { handler as fooHandler } from "./utils";
core.tsxtsximport { 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.knknotmain view MainView -> <div>Welcome!</div>;
view.knknotmain view MainView -> <div>Welcome!</div>;
constants.knknotmain const otherConst = 100;
constants.knknotmain const otherConst = 100;
core.knknotimport MainView from "@/views";import mainConst, { otherConst } from "@/constants";const truthy = mainConst == otherConst;
core.knknotimport MainView from "@/views";import mainConst, { otherConst } from "@/constants";const truthy = mainConst == otherConst;
view.tsxtsxexport default function MainView() {return <div>Welcome!</div>;}
view.tsxtsxexport default function MainView() {return <div>Welcome!</div>;}
constants.tstsxexport const otherConst = 100;export default otherConst;
constants.tstsxexport const otherConst = 100;export default otherConst;
core.tsxtsximport MainView from "./views";import mainConst, { otherConst } from "./constants";const truthy = mainConst == otherConst;
core.tsxtsximport MainView from "./views";import mainConst, { otherConst } from "./constants";const truthy = mainConst == otherConst;