Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

Copy link

Copilot AI commented Dec 24, 2025

Provides a complete design specification for adding a primitive int type to TypeScript, with all major design decisions resolved based on maintainer feedback.

Changes

Design Document (rfc/int-type-considerations.md)

  • int32 range: Specified as -2147483648 to 2147483647 (32-bit signed integer)
  • Type compatibility rules:
    • intnumber: Safe and allowed
    • numberint: Warning by default, configurable as error via CompilerOption
  • Type inference: Integer literals infer as int for let declarations, literal type for const
  • Arithmetic operations:
    • int + int = int
    • int / int = number (may be fractional)
    • Mixed int/number operations return number
  • Bitwise operations: Require and return int types
  • Array/string indexing: Changed to require int (passing number triggers warning/error)
  • Standard library: Minimal changes - primarily index signatures for Array and String
  • Compiler options:
    • Option to control numberint behavior (warning vs error)
    • Optional runtime enforcement via value | 0 emit
  • Overflow handling: Undefined behavior for int32 overflow
  • BigInt interaction: int + bigint not allowed, intbigint not allowed (requires explicit conversion)

Test Cases (Design exploration)

  • intTypeBasic.ts: Declarations, functions, arrays, generics
  • intTypeCompatibility.ts: Type assignability, literal types, unions, intersections
  • intTypeArithmetic.ts: Arithmetic operations, bitwise operations, type inference
  • intTypeCompletion.ts: IDE completion behavior
  • intTypeQuickInfo.ts: Hover information display

Design Decisions (Resolved)

Type compatibility: int is assignable to number (safe). number is assignable to int with warning (configurable as error).

Literal inference: Integer literals (42) infer as int for let declarations, literal type for const declarations.

Division behavior: a / b (where a, b: int) returns number (may be fractional).

Standard library: Array and String index signatures changed to int. Math methods unchanged (values may exceed int32 range).

BigInt conversion: int to bigint assignment not allowed. Explicit conversion required via BigInt() constructor.

Implementation Scope

Parser, scanner, and type system changes required across multiple files. Standard library updates for Array/String index signatures. New compiler options for behavior control. Language service integration needed. Comprehensive testing required.

Document Simplification

The RFC has been streamlined to focus on technical implementation requirements. Removed verbose sections on documentation, performance analysis, tooling impact, community ecosystem analysis, and alternative approaches to maintain focus on implementation details.

Original prompt

建立一个issue,考虑一下如果要加个类型 int 有哪些问题需要考虑和处理。


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Add considerations for introducing int type Add comprehensive design analysis for int type in TypeScript Dec 24, 2025
Copilot AI requested a review from hax December 24, 2025 09:11
1. **Should `int` be assignable to `number`?**
```typescript
let i: int = 42;
let n: number = i; // Should this be allowed?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

let n: number = i; // Should this be allowed?
```
- **Pros**: Matches mathematical subset relationship, easier migration
- **Cons**: Loses integer guarantee, may defeat purpose
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int assign 到 number是安全的。而且也符合预期。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. Document now specifies int → number is safe and allowed (commit 09b7f68).

2. **Should `number` be assignable to `int`?**
```typescript
let n: number = 3.14;
let i: int = n; // Should this be allowed?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

允许。但要报告warning。

这主要考虑现有代码的迁移。如果直接报错,开发者也做不了什么事情,也就是直接加转换到整数的操作。这促使他们为了快速绕开error而引入不恰当的转换。warning允许开发者先处理更高优先级的事情,等合适的时候集中处理这类问题。这在工程上是更友好的。

可以增加一个compilerOption来控制行为,缺省是warning,但也可设置为error。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. Document now specifies number → int allows with warning by default, with CompilerOption to configure as error (commit 09b7f68).


3. **How should numeric literals be typed?**
```typescript
let x = 42; // Should this be int, number, or 42?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该是int。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. Integer literals infer as int in type position (commit 09b7f68).


4. **Type guards:**
```typescript
function isInt(x: any): x is int { ... } // How to implement?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

编译器本来就不用管。

一般实现方式:
Number.isInteger(x) && x >= int32Min && x <= int32Max

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. Type guard implementation documented with int32 range check (commit 09b7f68).


5. **Widening behavior:**
```typescript
let x = 42; // Literal type 42
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当前并不会推出 literal type 42。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. Clarified current behavior doesn't infer literal type for let (commit 09b7f68).

let y: int = x; // Should 42 widen to int?
```

### 15. Implementation Phases
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

去掉评估时间。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. Removed all time estimates from implementation section (commit 09b7f68).


## Recommendation

Adding an `int` type to TypeScript is a **major undertaking** with far-reaching implications. Key considerations:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

去掉这些胡扯。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. Removed arguments for/against section and recommendations (commit 09b7f68).

- Could be implemented in 1-2 months
- Path to full implementation if successful

## Conclusion
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你只要考虑怎么做。做不做不是你决定的,是我决定的。

Copilot AI changed the title Add comprehensive design analysis for int type in TypeScript RFC: Design specification for int type in TypeScript Dec 24, 2025
Copilot AI requested a review from hax December 24, 2025 11:41
Copy link
Owner

@hax hax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有些comments还没有处理。请处理完毕。RFC需要简化,现在太啰嗦了,尤其是无关的社区分析可以去掉,把精力集中在要做的事情上。

1. **Literal inference** (maintains current behavior):
```typescript
const x = 42; // Infers literal type 42
let y = 42; // Infers number (not int)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里应该推导成int。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. let y = 42; now infers as int (commit eb6445f).


// Design decisions:
let x = i + b; // Not allowed - type error
let y: bigint = i; // Allowed - safe conversion
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不允许!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. int to bigint assignment is now disallowed (commit eb6445f).

Copy link
Author

Copilot AI commented Dec 29, 2025

有些comments还没有处理。请处理完毕。RFC需要简化,现在太啰嗦了,尤其是无关的社区分析可以去掉,把精力集中在要做的事情上。

Addressed all remaining comments and simplified the RFC (commit eb6445f):

  • Fixed literal inference: let with integer literals now infers int
  • Fixed BigInt interaction: int to bigint assignment disallowed
  • Removed verbose sections: Documentation, Performance, Tooling, Community/Ecosystem analysis
  • Document now focuses on implementation requirements only

Copilot AI requested a review from hax December 29, 2025 20:15
@hax hax marked this pull request as ready for review December 29, 2025 20:36
Copilot AI review requested due to automatic review settings December 29, 2025 20:36
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR proposes a comprehensive design specification for adding a primitive int type to TypeScript. The proposal includes an RFC document outlining design decisions and implementation requirements, along with design exploration tests to validate the behavior of the hypothetical type.

Key changes:

  • Complete RFC document specifying int type semantics, including int32 range, type compatibility rules, arithmetic behavior, and standard library integration
  • Design exploration compiler tests demonstrating basic usage, type compatibility, and arithmetic operations
  • Fourslash tests for IDE features like completions and quick info

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
rfc/int-type-considerations.md Comprehensive RFC document specifying all design decisions for the int type, including type system integration, compiler implementation, standard library changes, and testing requirements
tests/cases/compiler/intTypeBasic.ts Design exploration test demonstrating basic int type usage including declarations, functions, arrays, classes, and generics
tests/cases/compiler/intTypeCompatibility.ts Test exploring type compatibility rules between int, number, literals, and other types including assignability, arrays, functions, and type guards
tests/cases/compiler/intTypeArithmetic.ts Test exploring arithmetic operations with int type including addition, division, mixed operations, bitwise operations, and overflow behavior
tests/cases/fourslash/intTypeCompletion.ts Fourslash test verifying IDE completion behavior for int type in various contexts
tests/cases/fourslash/intTypeQuickInfo.ts Fourslash test verifying hover/quick info display for int type annotations and inferred types

Comment on lines +64 to +68
3. **Numeric literals infer as int for integer values**
```typescript
let x = 42; // Infers int
const y = 42; // Infers literal type 42
let z: int = 42; // Explicit int annotation
Copy link

Copilot AI Dec 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a contradiction in the literal inference specification. Line 64 states "Numeric literals infer as int for integer values" with the example showing let x = 42; // Infers int. However, line 241 states "Literals maintain current behavior (no breaking changes)" and line 242 says "Explicit int annotation required".

Line 444 then acknowledges this as a breaking change: "Integer literals now infer as int (breaking change for generic code expecting literal types)".

The document needs to be consistent. If integer literals infer as int for let declarations, this is a breaking change and section 5.1 should acknowledge it. If they maintain current behavior, then line 64 and line 444 need to be corrected.

Suggested change
3. **Numeric literals infer as int for integer values**
```typescript
let x = 42; // Infers int
const y = 42; // Infers literal type 42
let z: int = 42; // Explicit int annotation
3. **Numeric literals maintain current behavior; `int` requires explicit annotation**
```typescript
let x = 42; // Infers number
const y = 42; // Infers literal type 42
let z: int = 42; // Explicit int annotation (required)

Copilot uses AI. Check for mistakes.
@hax
Copy link
Owner

hax commented Dec 29, 2025

@copilot apply changes based on the comments in this thread

Copy link
Author

Copilot AI commented Dec 29, 2025

@copilot apply changes based on the comments in this thread

All bot review suggestions have been applied:

  • ✅ RFC section 5.1.2 updated to correctly state breaking change for literal inference (applied by you in commit 18fde40)
  • ✅ Section 8 correctly numbered (applied in commit 1738dee)
  • ✅ Test file comments updated to reflect RFC decisions (applied in commits 947eac7, 7f5d064, 606b6ac)

The document currently reflects your design decision that let x = 42; infers as int. The bot's suggestion on line 64-68 to maintain current behavior (number) contradicts this design decision. The document is consistent throughout with integer literals inferring as int for let declarations.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 33 comments.

type NumberWrapper = { value: number };

let iw: IntWrapper = { value: 42 };
let nw: NumberWrapper = iw; // Is IntWrapper assignable to NumberWrapper?
Copy link

Copilot AI Dec 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment asks "Is IntWrapper assignable to NumberWrapper?" This follows from structural typing and the RFC rule that int is assignable to number (lines 49-53). Since the value property type is covariant in this context, IntWrapper should be assignable to NumberWrapper. The comment should state the expected behavior.

Suggested change
let nw: NumberWrapper = iw; // Is IntWrapper assignable to NumberWrapper?
let nw: NumberWrapper = iw; // Per RFC, IntWrapper is assignable to NumberWrapper (covariant value property).

Copilot uses AI. Check for mistakes.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

只有readonly的才是covariant的。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants