一键导入
different-variables-types
Use when tempted to reuse variables. Use when variable changes type. Use when using union types for multiple purposes.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Use when tempted to reuse variables. Use when variable changes type. Use when using union types for multiple purposes.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Use when defining global types. Use when augmenting window. Use when typing environment variables. Use when working with build-time constants. Use when configuring type definitions.
Use when migrating JavaScript to TypeScript. Use when gradually adopting TypeScript. Use when working with mixed codebases. Use when converting large projects. Use when teams are learning TypeScript.
Use when writing asynchronous code. Use when tempted to use callbacks. Use when composing multiple async operations.
Use when creating types from example data. Use when types don't match all cases. Use when API responses vary.
Use when writing type annotations on variables. Use when TypeScript can infer the type. Use when code feels cluttered with types.
Use when defining array-like types. Use when tempted to use number as index type. Use when understanding array keys.
| name | different-variables-types |
| description | Use when tempted to reuse variables. Use when variable changes type. Use when using union types for multiple purposes. |
Don't reuse variables for different purposes.
In JavaScript, you can assign any value to any variable. But in TypeScript, a variable's type is generally fixed. Using different variables for different concepts improves type safety and code clarity.
One variable, one type, one purpose.
Different concepts deserve different names.
Remember:
let productId = "12-34-56";
fetchProduct(productId); // Expects string
productId = 123456;
// ~~~~~~~ Type 'number' is not assignable to type 'string'
fetchProductBySerialNumber(productId);
// Expects number
TypeScript infers productId as string from the first assignment.
// Works, but creates problems
let productId: string | number = "12-34-56";
fetchProduct(productId);
productId = 123456; // OK
fetchProductBySerialNumber(productId); // OK
// But now everywhere you use productId, you need type guards:
if (typeof productId === 'string') {
// ...
}
Union types are harder to work with and obscure the code's intent.
const productId = "12-34-56";
fetchProduct(productId);
const serial = 123456;
fetchProductBySerialNumber(serial);
Benefits:
const// let: type can be reassigned
let x = 'hello';
x = 'goodbye'; // OK
x = 42; // Error: different type
// const: value cannot change
const y = 'hello';
y = 'goodbye'; // Error: cannot reassign const
// No type ambiguity ever
const makes code easier for both humans and TypeScript to understand.
const productId = "12-34-56";
fetchProduct(productId);
{
const productId = 123456; // Different variable, same name
fetchProductBySerialNumber(productId);
}
This works because they're different variables in different scopes. But it's confusing for humans - prefer different names instead.
TypeScript allows type narrowing (getting more specific):
let x: string | number = getValue();
if (typeof x === 'string') {
x // Type: string (narrowed)
x.toUpperCase(); // OK
}
This is different from trying to widen a type (getting less specific), which doesn't work:
let x = 'hello'; // Type: string
x = 42; // Error: can't widen to string | number
// Bad: reusing 'result' for different things
let result = await fetchUser(id);
processUser(result);
result = await fetchPosts(id); // Error: different type!
processPosts(result);
// Good: descriptive names for each
const user = await fetchUser(id);
processUser(user);
const posts = await fetchPosts(id);
processPosts(posts);
Pressure: "It's just temporary, I'll reassign it"
Response: Use a new variable. It's clearer and type-safe.
Action: Create a new const with a descriptive name.
Pressure: "I'll just use string | number"
Response: Union types add complexity everywhere you use the variable.
Action: Separate variables with simple types are easier to work with.
let x: A | B where A and B serve different purposestemp or result being reused| Excuse | Reality |
|---|---|
| "It saves a variable" | Variables are cheap; bugs are expensive |
| "Same data, different format" | Different formats = different variables |
| "Less code" | More clarity > less code |
// DON'T: Reuse variable for different purposes
let id = "abc";
fetchByString(id);
id = 123; // Error
// DON'T: Use union just to allow reassignment
let id: string | number = "abc";
// DO: Different variables for different concepts
const stringId = "abc";
const numericId = 123;
Different concepts deserve different variables.
TypeScript types don't change (except narrowing). Embrace this by using separate variables for separate purposes. Your code becomes clearer, types simpler, and bugs rarer.
Based on "Effective TypeScript" by Dan Vanderkam, Item 19: Use Different Variables for Different Types.