summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--compiler/cpp/src/thrift/generate/t_js_generator.cc19
1 files changed, 17 insertions, 2 deletions
diff --git a/compiler/cpp/src/thrift/generate/t_js_generator.cc b/compiler/cpp/src/thrift/generate/t_js_generator.cc
index 426b0e29d..5fcac1659 100644
--- a/compiler/cpp/src/thrift/generate/t_js_generator.cc
+++ b/compiler/cpp/src/thrift/generate/t_js_generator.cc
@@ -910,7 +910,14 @@ void t_js_generator::generate_js_struct_definition(ostream& out,
}
if (gen_ts_) {
string ts_access = gen_node_ ? "public " : "";
- f_types_ts_ << ts_indent() << ts_access << (*m_iter)->get_name() << ts_get_req(*m_iter) << ": "
+ string member_name = (*m_iter)->get_name();
+
+ // Special case. Exceptions derive from Error, and error has a non optional message field.
+ // Ignore the optional flag in this case, otherwise we will generate a incompatible field
+ // in the eyes of typescript.
+ string optional_flag = is_exception && member_name == "message" ? "" : ts_get_req(*m_iter);
+
+ f_types_ts_ << ts_indent() << ts_access << member_name << optional_flag << ": "
<< ts_get_type((*m_iter)->get_type()) << ";" << endl;
}
}
@@ -2802,8 +2809,16 @@ std::string t_js_generator::ts_function_signature(t_function* tfunction, bool in
str = tfunction->get_name() + "(";
+ bool has_written_optional = false;
+
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
- str += (*f_iter)->get_name() + ts_get_req(*f_iter) + ": " + ts_get_type((*f_iter)->get_type());
+ // Ensure that non optional parameters do not follow optional parameters
+ // E.g. public foo(a: string, b?: string; c: string) is invalid, c must be optional, or b non-optional
+ string original_optional = ts_get_req(*f_iter);
+ string optional = has_written_optional ? "?" : original_optional;
+ has_written_optional = has_written_optional || optional.size() > 0;
+
+ str += (*f_iter)->get_name() + optional + ": " + ts_get_type((*f_iter)->get_type());
if (f_iter + 1 != fields.end() || (include_callback && fields.size() > 0)) {
str += ", ";