-
Notifications
You must be signed in to change notification settings - Fork 38
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
Add “Composing types” #60
Conversation
character: 'friendly' | 'grumpy', | ||
} | ||
|
||
// This will never be satisfied: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about "This is an rtype error: Interface type collision:"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, for the user it would be perfect if we detected type collisions statically. But such a promise might be risky on our side as there’s no obvious way to detect some conflicts. Like a: /^[A-Z]+$/
and a: /^[a-z]+$/
or n: (number) => number < 0
vs n: (number) => number > 10
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think mismatched literals are conflicts by definition, don't you? If they're the same type, they should be the same literal, shouldn't they?
We shouldn't try to infer compatible types for literal definitions. Right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that sounds reasonable and definitely makes things simpler. Fair enough. I’ll push an update ASAP.
(Though /^\w/
and /^[abc]/
are compatible. I can think of lots of other examples.)
Great work so far! |
So can I, but is there an easy way to detect compatible types like this safely and automatically, for all compatible cases? Also note, this is compatible one way, meaning all matches of |
Nope, that’s what I meant too. Your idea is very good! |
Does that sound better? |
65646fb
to
99aa1d2
Compare
Also, I’m wondering about one more implication of this syntax. I have the following in a project: interface Integer
(number) => (
typeof number === 'number' &&
number === parseInt(number, 10)
);
interface PositiveInteger
(number) => (
typeof number === 'number' &&
number === parseInt(number, 10) &&
number > 0
); Since we can merge types by spreading them out within an interface block, should this mean the same? interface Integer
(number) => (
typeof number === 'number' &&
number === parseInt(number, 10)
);
interface Positive
(number) => (
typeof number === 'number' &&
number > 0
);
interface PositiveInteger { ...Integer, ...Positive } |
interface Integer
(number) => (
typeof number === 'number' &&
number === parseInt(number, 10)
);
interface Positive
(number) => (
typeof number === 'number' &&
number > 0
);
interface PositiveInteger { ...Integer, ...Positive } Yeah, I like this! =) |
Are we adding an example like the one above? |
Hi @ericelliott, I’ve been busy for a while – sorry. One thing bothers me though. In the same section we say
Merging predicate literals like How about making the static check opt-out? Some things (like merging literals) could just trigger a “you’re on your own” warning. |
I want to see this in the spec. I think we can sidestep the issue for now by making it an error if you try to merge literals. |
Alright! I don’t think there were any other open issues. Should I merge? |
Obviously, resolve the conflicts first.. =) |
Fixes #9.
This is also my contribution to the discussion on interface collisions. I think it should be valid to override more generic types like
String
with more specific ones like/^[a-z]+$/
.