🍋
Menu
Developer

Type Coercion

The automatic or implicit conversion of a value from one data type to another (e.g. string to number).

技术细节

Type Coercion encodes backward-compatibility promises in the version number. MAJOR changes indicate breaking API changes, MINOR adds backward-compatible features, and PATCH fixes bugs without API changes. Pre-release versions use hyphens (1.0.0-alpha.1) and build metadata uses plus signs (1.0.0+20260308). The caret (^1.2.3) and tilde (~1.2.3) range operators in package managers interpret version constraints differently: caret allows minor updates, tilde allows only patch updates.

示例

```javascript
// Parse semantic version
const [major, minor, patch] = '2.4.1'.split('.').map(Number);

// Version comparison
function semverCompare(a, b) {
  const pa = a.split('.').map(Number);
  const pb = b.split('.').map(Number);
  for (let i = 0; i < 3; i++) {
    if (pa[i] !== pb[i]) return pa[i] - pb[i];
  }
  return 0;
}
```

相关工具

相关术语