Casting between unrelated types should be a compile time error
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 5.4k
- Forks
- 215
- Avg merge
- 2h 9m
- Merged PRs (30d)
- 27
Description
Feature Spec
Currently unsafeCast allows for casting between any two types. But sometimes casting is never valid (e.g. from str to num, or from Json to `cloud.Bucket). By limiting to "valid" casts like Java and other languages do, we can prevent several kinds of bugs.
Examples of what safer casting could look like:
let a1: constructs.IConstruct = // ...
let b1: cloud.Bucket = unsafeCast(a1); // OK (this solves the use case from the original issue)
let a2: cloud.Bucket = // ...
let b2 = unsafeCast(a2); // OK (x is type `cloud.Bucket`)
let a3: cloud.Bucket = // ...
let b3: constructs.IConstruct = unsafeCast(a3); // OK. compiler warning: unsafeCast is unnecessary since type "Bucket" already implements interface "IConstruct"
let a4: MutMap<Array<str>> = // ...
let b4: Map<MutArray<str>> = unsafeCast(a4); // OK (collection types can change between their mutable and immutable variants)
let a5: str = // ...
let b5: num = unsafeCast(a5); // error: cannot perform cast since types "num" and "str" have nothing in common
let a6: inflight (str): void = // ...
let b6: inflight (str, num): bool = unsafeCast(a6); // OK
let a7: Json = // ...
let b7: cloud.Bucket = unsafeCast(a7); // error: cannot perform cast since types "cloud.Bucket" and "Json" have nothing in common
let a8: Json = // ...
let b8: Map<str> = unsafeCast(a8); // OK. compiler warning: consider using `Map<str>.fromJson()` instead
Use Cases
Preventing bugs. For example, if I tried downcasting from IStorage to MyStorageImpl, then I was probably expecting that MyStorageImpl actually implements IStorage -- but if the class doesn't implement that interface, I would prefer to get a compile time error instead.
Implementation Notes
No response
Component
Compiler
Community Notes
- Please vote by adding a 👍 reaction to the issue to help us prioritize.
- If you are interested to work on this issue, please leave a comment.
- If this issue is labeled needs-discussion, it means the spec has not been finalized yet. Please reach out on the #dev channel in the Wing Slack.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue identifies the compiler and the unsafeCast operation but names no files, tests, or entry points. Start by locating unsafeCast handling and the compiler's type-compatibility checks, then clarify the accepted cast rules with maintainers. Done means unrelated types are rejected at compile time while the listed valid cases remain supported, with compiler tests covering them.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100