Skip to content

Commit f079fa7

Browse files
committed
docs: update tw5-typed setup guidance
1 parent fcee003 commit f079fa7

7 files changed

Lines changed: 105 additions & 35 deletions

File tree

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,24 @@ Don't worry, even if you are not good at programming, it doesn't matter, Modern.
3535

3636
There's a lot of work here, but it's just Out-of-the-Box for you. Use it and get started on your modern TiddlyWiki plugin development journey!
3737

38+
## TypeScript Setup
39+
40+
Modern.TiddlyDev now loads [tw5-typed](https://github.com/tiddly-gittly/TW5-Typed) in the supported way:
41+
42+
```json
43+
{
44+
"compilerOptions": {
45+
"types": ["node", "tw5-typed"]
46+
}
47+
}
48+
```
49+
50+
And the template keeps a project ambient entry in `src/global.d.ts`:
51+
52+
```typescript
53+
/// <reference types="tw5-typed" />
54+
```
55+
56+
This makes both `import type { ... } from 'tiddlywiki'` and the global `$tw` available to TypeScript and VS Code. Do not put `tw5-typed` into `typeRoots`; it is a normal package with its own `types` entry, and using `typeRoots` there can stop the editor from resolving the declarations correctly.
57+
3858
![](src/doc/wysiwyg-demo.gif)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"postcss": "^8.5.6",
4848
"rimraf": "^6.1.0",
4949
"ts-node": "^10.9.2",
50-
"tw5-typed": "^0.6.8",
50+
"tw5-typed": "^1.1.4",
5151
"typescript": "^5.9.3"
5252
},
5353
"dependencies": {

pnpm-lock.yaml

Lines changed: 43 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/doc/tutorials/en/003-typescript-dev.tid

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,24 @@ A widget is a rendering unit for <$text text="TiddlyWiki" />, which is what you
1313

1414
We want to create a wdiget that displays a random number and re-randomizes it when clicked, and we name this widget <$text text="RandomNumber" />. To achieve this, we add a `RandomNumberWidget.ts` file and a `RandomNumberWidget.ts.meta` file to the plugin development directory.
1515

16+
Before writing widget code, make sure the project loads [[tw5-typed|https://github.com/tiddly-gittly/TW5-Typed]] in the supported way. In `tsconfig.json`, use:
17+
18+
```json
19+
{
20+
"compilerOptions": {
21+
"types": ["node", "tw5-typed"]
22+
}
23+
}
24+
```
25+
26+
And in `src/global.d.ts`, keep an ambient entry like:
27+
28+
```typescript
29+
/// <reference types="tw5-typed" />
30+
```
31+
32+
This gives you both `import type { ... } from 'tiddlywiki'` and the global `$tw` in VS Code. Do not use `typeRoots` for `tw5-typed`; that can cause the editor to lose the `tiddlywiki` module declaration.
33+
1634
First write the following in `RandomNumberWidget.ts.meta`;
1735

1836
```yaml
@@ -55,7 +73,7 @@ Execute `pnpm run dev` to compile the source file, open a browser, create a tidd
5573

5674
Note: The framework also supports compilation of cjs, mjs, jsx, tsx files.
5775

58-
Note: The related <$text text="TypeScript" /> type support is done by [[tw5-typed|https://github.com/tiddly-gittly/TW5-Typed]], which is not very complete, so some manual type annotations are needed as above. I welcome your help to improve tw5-typed!
76+
Note: The related <$text text="TypeScript" /> type support is done by [[tw5-typed|https://github.com/tiddly-gittly/TW5-Typed]]. It already provides the `tiddlywiki` module and global `$tw`, but TiddlyWiki's API surface is huge, so you will still occasionally need explicit annotations in your own code. Improvements to tw5-typed are always welcome.
5977

6078
! Importing other resources in <$text text="TypeScript" /> scripts
6179

src/doc/tutorials/zh/003-typescript-dev.tid

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,24 @@ widget 是 <$text text="TiddlyWiki" /> 的渲染单位,也就是你看到的
1515

1616
我们希望创建这样一个 wdiget:其显示一个随机数字,并在点击其时重新随机,我们把这个 widget 其名为 <$text text="RandomNumber" />。为了达到这样的目的,我们在插件开发目录下添加一个 `RandomNumberWidget.ts` 文件和 `RandomNumberWidget.ts.meta` 文件。
1717

18+
在写 Widget 代码前,先确保项目用正确方式加载了 [[tw5-typed|https://github.com/tiddly-gittly/TW5-Typed]]。在 `tsconfig.json` 里应当使用:
19+
20+
```json
21+
{
22+
"compilerOptions": {
23+
"types": ["node", "tw5-typed"]
24+
}
25+
}
26+
```
27+
28+
并且在 `src/global.d.ts` 里保留一个环境声明入口:
29+
30+
```typescript
31+
/// <reference types="tw5-typed" />
32+
```
33+
34+
这样 VS Code 才能稳定拿到 `import type { ... } from 'tiddlywiki'` 以及全局 `$tw` 的类型。不要把 `tw5-typed` 写进 `typeRoots`,否则编辑器有可能反而丢失 `tiddlywiki` 的模块声明。
35+
1836
首先在 `RandomNumberWidget.ts.meta` 中写如下内容;
1937

2038
```yaml
@@ -57,7 +75,7 @@ exports.random = RandomNumber;
5775

5876
注:框架同时也支持对 cjs、mjs、jsx、tsx 文件的编译。
5977

60-
注:相关 <$text text="TypeScript" /> 类型支持是由 [[tw5-typed|https://github.com/tiddly-gittly/TW5-Typed]] 完成的,由于完成度不高,所以需要像上面一样,做一些手动的类型注解。欢迎各位大佬帮忙完善 tw5-typed
78+
注:相关 <$text text="TypeScript" /> 类型支持由 [[tw5-typed|https://github.com/tiddly-gittly/TW5-Typed]] 提供。它已经提供了 `tiddlywiki` 模块和全局 `$tw`,但 TiddlyWiki 的 API 面很大,所以在自己的代码里偶尔仍然需要补一些显式类型注解。也欢迎继续完善 tw5-typed
6179

6280
! 在 <$text text="TypeScript" /> 脚本中导入其他资源
6381

src/global.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/// <reference types="tw5-typed" />
2+
13
declare module '*.css' {
24
const content: string;
35
export default content;

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"jsx": "react-jsx",
66
"baseUrl": "./",
77
"noEmit": true,
8-
"typeRoots": ["node_modules/@types", "node_modules/tw5-typed"],
8+
"types": ["node", "tw5-typed"],
99
"lib": ["DOM", "ESNext"]
1010
},
1111
"include": ["src", "*.json", "wiki/*.info", "*.mjs", "*.js", "src/**/*.d.ts"]

0 commit comments

Comments
 (0)