能否在Dart中使用TypeScript的类型?附TypeScript示例代码
Great question! While Dart and TypeScript share some similarities in static typing, you can't directly use TypeScript's type syntax in Dart. However, Dart offers equivalent (and often more type-safe) ways to replicate the behavior you're showing with TypeScript's union types. Let's walk through the solutions based on your examples.
Dart 3.0+ (Modern Approach: Union Types)
Starting with Dart 3.0, the language officially supports union types using the | operator—this is nearly identical to TypeScript's literal union syntax! You can define a type alias for your string literals, then use it in function parameters.
Replacing your first TypeScript example
Here's how you'd rewrite your Speed union type and getRealSpeed function in Dart:
typedef Speed = 'very slow' | 'slow' | 'normal' | 'fast' | 'very fast'; int getRealSpeed(Speed speed) { switch (speed) { case 'very slow': return 10; case 'slow': return 20; case 'normal': return 30; case 'fast': return 40; case 'very fast': return 50; // Dart's type system ensures all cases are covered, so no default needed here! } }
Replacing your second TypeScript example
You can even use union types directly in parameter definitions without a type alias, just like in TS:
int getRealSpeed(String speed) => switch (speed) { 'slow' => 20, 'fast' => 40, 'unknown' => 0, _ => throw ArgumentError('Invalid speed value'), }; // Or with a type alias for clarity: typedef SpeedOption = 'slow' | 'fast' | 'unknown'; int getRealSpeed(SpeedOption speed) => switch (speed) { 'slow' => 20, 'fast' => 40, 'unknown' => 0, };
Notice that Dart's switch expression (with =>) makes the code concise, and the type system will enforce that you handle all possible union members if you omit the default case.
Pre-Dart 3.0 (Using Enums for Type Safety)
If you're working with an older Dart version that doesn't support union types, enums are the best type-safe alternative. They prevent invalid string values at compile time, which is even safer than TypeScript's literal unions (since TS can't fully enforce string values at runtime without extra checks).
Here's how to rewrite your example with an enum:
enum Speed { verySlow('very slow'), slow('slow'), normal('normal'), fast('fast'), veryFast('very fast'); final String label; const Speed(this.label); } int getRealSpeed(Speed speed) { switch (speed) { case Speed.verySlow: return 10; case Speed.slow: return 20; case Speed.normal: return 30; case Speed.fast: return 40; case Speed.veryFast: return 50; } }
Enums are great if you need to attach additional data (like the string label here) or want strict compile-time validation of allowed values.
Key Differences to Note
- TypeScript's union types are more flexible for ad-hoc literal sets, while Dart's union types (3.0+) offer the same flexibility with strong compile-time checks.
- Enums in Dart are fully type-safe and can carry extra metadata, making them a better choice for fixed, well-defined sets of values.
内容的提问来源于stack exchange,提问作者Leonid Veremchuk

