(typescript): Fix invalid optional members and argument generation

Fixes two cases where the optional flag `?` is generated incorrectly for typescript, leading to invalid build:

 - Non-optional function arguments after optional arguments
 - Exception types with optional message
diff --git a/compiler/cpp/src/thrift/generate/t_js_generator.cc b/compiler/cpp/src/thrift/generate/t_js_generator.cc
index 426b0e2..5fcac16 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 @@
     }
     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 @@
 
   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 += ", ";