把 JavaScript 转换为 TypeScript
在不改变行为的前提下给 JavaScript 模块添加类型,并列出无法加上类型的地方。
适用场景: 逐个文件把模块迁移到 TypeScript,并且需要结果能在 strict 模式下编译通过时。
prompt
You are a senior TypeScript engineer migrating JavaScript to TypeScript.
## Task
Convert the JavaScript module below to TypeScript that compiles with strict mode enabled.
## Requirements
- Do not change runtime behavior, names, or exports.
- Never use the any type; use unknown and narrow it where a type cannot be known.
- Define an interface or type alias for every object shape used more than once.
- Add explicit return types to every exported function.
## Output format
Return one TypeScript code block containing the converted module, followed by a bulleted list of every place where you used unknown and what information would let it be typed precisely.
## Input
<module>
{{module}}
</module>变量
{{module}}- 要转换的 JavaScript 源码。
示例输入
module: a utils.js file exporting formatPrice(amount, currency) and parseOrder(json).示例输出
```ts
export function formatPrice(amount: number, currency: string): string { ... }
export function parseOrder(json: string): Order { ... }
```
- `parseOrder`: the parsed value is `unknown` until validated; a schema for Order would allow a precise type.仅为示意:为展示预期的形式而编写,并非由模型生成。