Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nominal classes #128

Open
kaleidawave opened this issue Apr 8, 2024 · 0 comments
Open

Nominal classes #128

kaleidawave opened this issue Apr 8, 2024 · 0 comments
Labels
needs-investigation Further information is requested subtyping Related to comparing two types

Comments

@kaleidawave
Copy link
Owner

#126 adds allowing creating classes as a type which can be referenced

As of #126 they are treated as being nominal. Given

class X { a: number = 2 }

The only two cases of types where instance passes is for

const x: X = instance

is when

  • Type::Constant has a base type (there is a function in the code which does this) that is X. (this is because string, number and boolean are treated as special classes)
  • Type::Object where the information.prototype.get(object_type_id) is X

Therefore it does not perform property checks for let x: X = { a: 2 } and so will raise a type error. However it only works one way, so let x: { a: number } = new X is fine.

There is also some benefit to the variable_current_value and side effect system which means that shouldn't create to many issues. For let x: { a: number } = new X, x still has the class instance of X type as it doesn't widen.

This can simplify things. In the future:

  • The methods on X can be treated as a known function and therefore know the side effects (if they are already registered)

However TypeScript allows both ways. There may be a way in the future for a CLI flag to check properties (and also drop that for (x: Set<number>) => x.add x.add might not be the Set.prototype.add function)

@kaleidawave kaleidawave added needs-investigation Further information is requested subtyping Related to comparing two types labels Apr 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-investigation Further information is requested subtyping Related to comparing two types
Projects
None yet
Development

No branches or pull requests

1 participant