npx skills add https://github.com/mindrally/skills --skill deno-typescript您是一位 Deno 和 TypeScript 开发专家,深谙利用 Deno 的原生 TypeScript 支持和内置工具构建安全、现代化应用程序。
any 类型 - 应创建必要的类型isLoading、hasError、canDelete广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
as const 的常量对象readonlysrc/
routes/
{resource}/
mod.ts
handlers.ts
validators.ts
middleware/
auth.ts
logger.ts
services/
{domain}_service.ts
types/
mod.ts
utils/
mod.ts
deps.ts
main.ts
deno.json
使用带有显式文件扩展名的 ES 模块
使用 deps.ts 模式进行集中式依赖管理
从 URL 导入或在 deno.json 中使用导入映射
使用 JSR (jsr.io) 获取 Deno 原生包
// deps.ts - 集中管理依赖 export { serve } from "https://deno.land/std@0.208.0/http/server.ts"; export { z } from "https://deno.land/x/zod@v3.22.4/mod.ts";
// 在 deno.json 中使用导入映射 { "imports": { "std/": "https://deno.land/std@0.208.0/", "hono": "https://deno.land/x/hono@v3.11.7/mod.ts" } }
Deno 默认是安全的。仅请求必要的权限:
# 使用特定权限运行
deno run --allow-net --allow-read=./data --allow-env main.ts
# 权限标志
--allow-net=example.com # 访问特定域名的网络权限
--allow-read=./path # 文件读取权限
--allow-write=./path # 文件写入权限
--allow-env=API_KEY # 环境变量访问权限
--allow-run=cmd # 子进程执行权限
// 编程式权限请求
const status = await Deno.permissions.request({ name: "net", host: "api.example.com" });
if (status.state === "granted") {
// 网络访问已授权
}
// 简单的 HTTP 服务器
Deno.serve({ port: 8000 }, (req) => {
const url = new URL(req.url);
if (url.pathname === "/api/users" && req.method === "GET") {
return Response.json({ users: [] });
}
return new Response("Not Found", { status: 404 });
});
import { Hono } from "https://deno.land/x/hono/mod.ts";
const app = new Hono();
app.get("/", (c) => c.text("Hello Deno!"));
app.get("/api/users", (c) => c.json({ users: [] }));
Deno.serve(app.fetch);
// routes/index.tsx
import { PageProps } from "$fresh/server.ts";
export default function Home(props: PageProps) {
return (
<div>
<h1>Welcome to Fresh</h1>
</div>
);
}
// routes/api/users.ts
import { Handlers } from "$fresh/server.ts";
export const handler: Handlers = {
async GET(_req, _ctx) {
const users = await getUsers();
return Response.json(users);
},
};
// 使用 Deno KV(内置键值存储)
const kv = await Deno.openKv();
// 设置值
await kv.set(["users", "1"], { name: "John", email: "john@example.com" });
// 获取值
const result = await kv.get(["users", "1"]);
console.log(result.value);
// 列出值
const entries = kv.list({ prefix: ["users"] });
for await (const entry of entries) {
console.log(entry.key, entry.value);
}
// 访问环境变量(需要 --allow-env 权限)
const apiKey = Deno.env.get("API_KEY");
// 使用 dotenv
import { load } from "https://deno.land/std/dotenv/mod.ts";
const env = await load();
// user_test.ts
import { assertEquals, assertRejects } from "https://deno.land/std/assert/mod.ts";
import { describe, it, beforeEach } from "https://deno.land/std/testing/bdd.ts";
import { getUser, createUser } from "./user_service.ts";
describe("User Service", () => {
beforeEach(() => {
// 设置
});
it("should create a user", async () => {
const user = await createUser({ name: "John", email: "john@example.com" });
assertEquals(user.name, "John");
});
it("should throw for invalid email", async () => {
await assertRejects(
() => createUser({ name: "John", email: "invalid" }),
Error,
"Invalid email"
);
});
});
// 运行测试
// deno test --allow-net --allow-read
# 格式化
deno fmt
# 代码检查
deno lint
# 类型检查
deno check main.ts
# 打包
deno bundle main.ts bundle.js
# 编译为可执行文件
deno compile --allow-net main.ts
# 生成文档
deno doc main.ts
# 依赖检查
deno info main.ts
{
"tasks": {
"dev": "deno run --watch --allow-net --allow-env main.ts",
"start": "deno run --allow-net --allow-env main.ts",
"test": "deno test --allow-net",
"lint": "deno lint",
"fmt": "deno fmt"
},
"imports": {
"std/": "https://deno.land/std@0.208.0/",
"@/": "./src/"
},
"compilerOptions": {
"strict": true,
"lib": ["deno.window"]
},
"lint": {
"rules": {
"tags": ["recommended"]
}
},
"fmt": {
"indentWidth": 2,
"singleQuote": true
}
}
class AppError extends Error {
constructor(
message: string,
public statusCode: number = 500,
public code: string = "INTERNAL_ERROR"
) {
super(message);
this.name = "AppError";
}
}
const handleRequest = async (req: Request): Promise<Response> => {
try {
return await processRequest(req);
} catch (error) {
if (error instanceof AppError) {
return Response.json(
{ error: error.message, code: error.code },
{ status: error.statusCode }
);
}
console.error(error);
return Response.json(
{ error: "Internal Server Error" },
{ status: 500 }
);
}
};
Deno 拥抱 Web 标准。请使用:
fetch() 用于 HTTP 请求Request 和 Response 对象URL 和 URLSearchParamsWeb Crypto API 用于加密Streams API 用于数据流处理FormData 用于多部分数据Deno.serve 实现高性能 HTTP每周安装次数
113
代码仓库
GitHub 星标数
42
首次出现
2026年1月25日
安全审计
安装于
opencode94
gemini-cli92
codex88
github-copilot84
claude-code81
cursor76
You are an expert in Deno and TypeScript development with deep knowledge of building secure, modern applications using Deno's native TypeScript support and built-in tooling.
any type - create necessary types insteadisLoading, hasError, canDeleteas constreadonly for immutable propertiessrc/
routes/
{resource}/
mod.ts
handlers.ts
validators.ts
middleware/
auth.ts
logger.ts
services/
{domain}_service.ts
types/
mod.ts
utils/
mod.ts
deps.ts
main.ts
deno.json
Use ES modules with explicit file extensions
Use deps.ts pattern for centralized dependency management
Import from URLs or use import maps in deno.json
Use JSR (jsr.io) for Deno-native packages
// deps.ts - centralized dependencies export { serve } from "https://deno.land/std@0.208.0/http/server.ts"; export { z } from "https://deno.land/x/zod@v3.22.4/mod.ts";
// Using import maps in deno.json { "imports": { "std/": "https://deno.land/std@0.208.0/", "hono": "https://deno.land/x/hono@v3.11.7/mod.ts" } }
Deno is secure by default. Request only necessary permissions:
# Run with specific permissions
deno run --allow-net --allow-read=./data --allow-env main.ts
# Permission flags
--allow-net=example.com # Network access to specific domains
--allow-read=./path # File read access
--allow-write=./path # File write access
--allow-env=API_KEY # Environment variable access
--allow-run=cmd # Subprocess execution
// Programmatic permission requests
const status = await Deno.permissions.request({ name: "net", host: "api.example.com" });
if (status.state === "granted") {
// Network access granted
}
// Simple HTTP server
Deno.serve({ port: 8000 }, (req) => {
const url = new URL(req.url);
if (url.pathname === "/api/users" && req.method === "GET") {
return Response.json({ users: [] });
}
return new Response("Not Found", { status: 404 });
});
import { Hono } from "https://deno.land/x/hono/mod.ts";
const app = new Hono();
app.get("/", (c) => c.text("Hello Deno!"));
app.get("/api/users", (c) => c.json({ users: [] }));
Deno.serve(app.fetch);
// routes/index.tsx
import { PageProps } from "$fresh/server.ts";
export default function Home(props: PageProps) {
return (
<div>
<h1>Welcome to Fresh</h1>
</div>
);
}
// routes/api/users.ts
import { Handlers } from "$fresh/server.ts";
export const handler: Handlers = {
async GET(_req, _ctx) {
const users = await getUsers();
return Response.json(users);
},
};
// Using Deno KV (built-in key-value store)
const kv = await Deno.openKv();
// Set a value
await kv.set(["users", "1"], { name: "John", email: "john@example.com" });
// Get a value
const result = await kv.get(["users", "1"]);
console.log(result.value);
// List values
const entries = kv.list({ prefix: ["users"] });
for await (const entry of entries) {
console.log(entry.key, entry.value);
}
// Access environment variables (requires --allow-env)
const apiKey = Deno.env.get("API_KEY");
// Using dotenv
import { load } from "https://deno.land/std/dotenv/mod.ts";
const env = await load();
// user_test.ts
import { assertEquals, assertRejects } from "https://deno.land/std/assert/mod.ts";
import { describe, it, beforeEach } from "https://deno.land/std/testing/bdd.ts";
import { getUser, createUser } from "./user_service.ts";
describe("User Service", () => {
beforeEach(() => {
// Setup
});
it("should create a user", async () => {
const user = await createUser({ name: "John", email: "john@example.com" });
assertEquals(user.name, "John");
});
it("should throw for invalid email", async () => {
await assertRejects(
() => createUser({ name: "John", email: "invalid" }),
Error,
"Invalid email"
);
});
});
// Run tests
// deno test --allow-net --allow-read
# Formatting
deno fmt
# Linting
deno lint
# Type checking
deno check main.ts
# Bundle
deno bundle main.ts bundle.js
# Compile to executable
deno compile --allow-net main.ts
# Documentation generation
deno doc main.ts
# Dependency inspection
deno info main.ts
{
"tasks": {
"dev": "deno run --watch --allow-net --allow-env main.ts",
"start": "deno run --allow-net --allow-env main.ts",
"test": "deno test --allow-net",
"lint": "deno lint",
"fmt": "deno fmt"
},
"imports": {
"std/": "https://deno.land/std@0.208.0/",
"@/": "./src/"
},
"compilerOptions": {
"strict": true,
"lib": ["deno.window"]
},
"lint": {
"rules": {
"tags": ["recommended"]
}
},
"fmt": {
"indentWidth": 2,
"singleQuote": true
}
}
class AppError extends Error {
constructor(
message: string,
public statusCode: number = 500,
public code: string = "INTERNAL_ERROR"
) {
super(message);
this.name = "AppError";
}
}
const handleRequest = async (req: Request): Promise<Response> => {
try {
return await processRequest(req);
} catch (error) {
if (error instanceof AppError) {
return Response.json(
{ error: error.message, code: error.code },
{ status: error.statusCode }
);
}
console.error(error);
return Response.json(
{ error: "Internal Server Error" },
{ status: 500 }
);
}
};
Deno embraces web standards. Use:
fetch() for HTTP requestsRequest and Response objectsURL and URLSearchParamsWeb Crypto API for cryptographyStreams API for data streamingFormData for multipart dataDeno.serve for high-performance HTTPWeekly Installs
113
Repository
GitHub Stars
42
First Seen
Jan 25, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode94
gemini-cli92
codex88
github-copilot84
claude-code81
cursor76
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
116,600 周安装