-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Design Meeting Notes, 5/27/2016 #8908
Comments
There are a lot of tools that use the Mozilla Parser API to represent ASTs (babel, esprima, escodegen, acorn, and their respective plugins, etc). That defines a interface Node {
type: string;
} Then all the node types extend that, e.g.: interface IfStatement extends Node {
type: "IfStatement";
test: Expression;
consequent: Statement;
alternate: Statement;
}
interface LogicalExpression extends Node {
type: "LogicalExpression";
operator: "||" | "&&";
left: Expression;
right: Expression;
} You can see a full set of parser API typings here. Similar to the meeting notes example, it would be great when dealing with these AST to be able to write: function foo(node: Node) {
if (node.type === 'Identifier') {
node.name;
}
else if (node.type === 'MemberExpression') {
node.object;
node.property;
}
else {
...
}
} |
Yup @yortus that's the general direction we're hoping to land in. |
Was there an outcome on |
I'd also like to reference #6062 in regard to the ADT work. Nothing has to be/should be specific to singleton types or enums - switching based on any kind of discriminating type structure should probably be possible. I could probably just clean that PR up and get feedback and we'd have what we want there. I also have a half done implementation of exactly this: interface PropertyAccess {
kind: SyntaxKind.PropertyAccess;
} based around my numeric literal types PR (though enum member types are mostly separate from them). Though most of my 'open questions' to myself from those resulted in questioning how they should be compatible with each other and numeric literals. I could clean that up a bit and divorce it from the numeric literal work if we're interested in it. |
@RyanCavanaugh not much actual discussion on it as much as the details on it to be honest. |
Narrowing within Function Expressions
const
Modifiers on Function Parametersconst
, you know for sure that you can definitely narrow.public const
?public readonly
does that?const
?i.e.
IIFEs
Repro from #8381
Enums & Algebraic Data Types
Simple approach is that you use singleton value types.
Currently, these singleton value types are limited to string literal types.
Gets more interesting with something like:
These need not be string literals - they could be numbers, enums.
Wouldn't it be nice if we could do this sort of thing with
SyntaxKind
in the compiler?Each node would define its own specific discriminator in its
kind
field.Problematically, you couldn't quite do this with the OO-style property inheritance we get (open-ended in OOP, close-ended in FP).
Ideally, you could do exhaustiveness checking with this, but
Node
is open-ended right now.So you'd do something like this:
What would the type of a
Node
's kind be now?SyntaxKind.ProperyAccess | SyntaxKind.CallExpression | ....
Is
SyntaxKind
equivalent to that union type?Not necessarily! That can be confusing!
So you'd have to do something like this:
So to avoid the
AllColors
example, you could imagine that we'd have a new kind of enum:TC39 convo on enums had symbols in mind.
DR: I think we could do ADT-style stuff without needing enum types yet.
DR: Problem is that the base-type/intermediate-types/union-type pattern is cumbersome - you've fixed the enum type, done nothing there.
The text was updated successfully, but these errors were encountered: