diff --git a/packages/nitrogen/src/index.ts b/packages/nitrogen/src/index.ts index 8a79106db..f10681394 100644 --- a/packages/nitrogen/src/index.ts +++ b/packages/nitrogen/src/index.ts @@ -24,7 +24,13 @@ let generatedSpecs = 0 const baseDirectory = getBaseDirectory() // The TS project -const project = new Project({}) +const project = new Project({ + compilerOptions: { + strict: true, + strictNullChecks: true, + noUncheckedIndexedAccess: true, + }, +}) project.addSourceFilesAtPaths(path.join(baseDirectory, '/**/*.nitro.ts')) // Loop through all source files to log them diff --git a/packages/nitrogen/src/syntax/Method.ts b/packages/nitrogen/src/syntax/Method.ts index f11de61f8..9980a468c 100644 --- a/packages/nitrogen/src/syntax/Method.ts +++ b/packages/nitrogen/src/syntax/Method.ts @@ -59,8 +59,7 @@ export class Method implements CodeNode { const [method] = args this.name = method.getSymbolOrThrow().getEscapedName() const returnType = method.getReturnType() - const isOptional = returnType.isNullable() - this.returnType = createType(returnType, isOptional) + this.returnType = createType(returnType, returnType.isNullable()) this.parameters = method.getParameters().map((p) => new Parameter(p)) } } diff --git a/packages/nitrogen/src/syntax/createType.ts b/packages/nitrogen/src/syntax/createType.ts index 8baff846d..eac2810e1 100644 --- a/packages/nitrogen/src/syntax/createType.ts +++ b/packages/nitrogen/src/syntax/createType.ts @@ -176,14 +176,17 @@ export function createType(type: TSMorphType, isOptional: boolean): Type { return new EnumType(typename, type) } else { // It consists of different types - that means it's a variant! - const variants = type.getUnionTypes().map((t) => createType(t, false)) - const isOptionalVariant = variants.some((t) => t.kind === 'null') - const variant = new VariantType(variants.filter((v) => v.kind !== 'null')) - if (isOptionalVariant) { - return new OptionalType(variant) - } else { - return variant + const variants = type + .getUnionTypes() + // Filter out any nulls or undefineds, as those are already treated as `isOptional`. + .filter((t) => !t.isNull() && !t.isUndefined() && !t.isVoid()) + .map((t) => createType(t, false)) + if (variants.length === 1) { + // It's just one type with undefined/null varians - so we treat it like a simple optional. + return variants[0]! } + + return new VariantType(variants) } } else if (type.isInterface()) { // It references another interface/type, either a simple struct, or another HybridObject diff --git a/packages/react-native-nitro-image/nitrogen/generated/shared/c++/HybridTestObjectSpec.hpp b/packages/react-native-nitro-image/nitrogen/generated/shared/c++/HybridTestObjectSpec.hpp index 3946b4201..da513feb9 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/shared/c++/HybridTestObjectSpec.hpp +++ b/packages/react-native-nitro-image/nitrogen/generated/shared/c++/HybridTestObjectSpec.hpp @@ -67,10 +67,10 @@ namespace margelo::nitro::image { virtual void setStringValue(const std::string& stringValue) = 0; virtual int64_t getBigintValue() = 0; virtual void setBigintValue(int64_t bigintValue) = 0; - virtual std::string getStringOrUndefined() = 0; - virtual void setStringOrUndefined(const std::string& stringOrUndefined) = 0; - virtual std::string getStringOrNull() = 0; - virtual void setStringOrNull(const std::string& stringOrNull) = 0; + virtual std::optional getStringOrUndefined() = 0; + virtual void setStringOrUndefined(const std::optional& stringOrUndefined) = 0; + virtual std::optional getStringOrNull() = 0; + virtual void setStringOrNull(const std::optional& stringOrNull) = 0; virtual std::optional getOptionalString() = 0; virtual void setOptionalString(const std::optional& optionalString) = 0; virtual double getValueThatWillThrowOnAccess() = 0; @@ -89,7 +89,7 @@ namespace margelo::nitro::image { virtual std::shared_ptr mapRoundtrip(const std::shared_ptr& map) = 0; virtual double funcThatThrows() = 0; virtual std::string tryOptionalParams(double num, bool boo, const std::optional& str) = 0; - virtual std::string tryMiddleParam(double num, bool boo, const std::string& str) = 0; + virtual std::string tryMiddleParam(double num, const std::optional>& boo, const std::string& str) = 0; virtual std::variant passVariant(const std::variant, std::vector>& either) = 0; virtual std::tuple flip(const std::tuple& tuple) = 0; virtual std::tuple passTuple(const std::tuple& tuple) = 0; @@ -103,7 +103,7 @@ namespace margelo::nitro::image { virtual std::future getValueFromJsCallback(const Func_std__future_std__string_& callback, const Func_void_std__string& andThenCall) = 0; virtual Car getCar() = 0; virtual bool isCarElectric(const Car& car) = 0; - virtual Person getDriver(const Car& car) = 0; + virtual std::optional getDriver(const Car& car) = 0; virtual std::shared_ptr createArrayBuffer() = 0; virtual double getBufferLastItem(const std::shared_ptr& buffer) = 0; virtual void setAllValuesTo(const std::shared_ptr& buffer, double value) = 0;