Skip to content

Commit a85d4a1

Browse files
committed
Fix type conversion bug
1 parent 2ec4845 commit a85d4a1

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

demo.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
function demo(a: string, b: number, c: boolean, d: any[]): string {
2-
return a + b;
2+
return a + b + String(c) + d.join("");
33
}
44

55
// This should work correctly
66
demo("hello", 42, true, ["world"]);
77

8-
// Let's uncomment this to see if we get a type error
9-
demo("hello", "42", true, ["world"]);
8+
// This should fail
9+
// demo("hello");
10+
// demo("hello", 0, true, ["world"], "extra");
11+
// demo("hello", 0, true, {});
12+
// demo(0, 0, true, []);

rust/src/compiler/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,11 +423,12 @@ impl TypeChecker {
423423
self.is_assignable_to(src_elem_type, tgt_elem_type)
424424
}
425425

426-
// Number is assignable to String due to coercion
427-
(Type::Number, Type::String) => true,
426+
// In TypeScript, numbers can be coerced to strings during string concatenation,
427+
// but a Number type is not assignable to a String parameter
428+
(Type::Number, Type::String) => false,
428429

429-
// Boolean can be converted to String in JS
430-
(Type::Boolean, Type::String) => true,
430+
// Similarly, booleans are not assignable to strings in TypeScript
431+
(Type::Boolean, Type::String) => false,
431432

432433
// Add more special cases as needed
433434
// ...

0 commit comments

Comments
 (0)