Is there any way to extend the type without creating a new type containing similar properties as given in Typescript interfaces?
as an example, I have type like this
public type AuditRecord = {
fileId: Text;
action: Action;
userId: Text;
timestamp: ?Time.Time;
organizationId: ?Text;
};
I wanted to create a new type with additional properties like
public type AuditOrgdto = {
...AuditRecord ,
date: ?Text;
};
posting here believing you guys might be able to help
Is there any way to do this?
I am having a type
public type DishType = {
name : Text;
dishTypeId : Nat;
parentDishTypeId : Nat;
description: Text;
};
and my data is
record {3; record {parentDishTypeId=0; name="Main Course"; description=""; dishTypeId=3}};
Now if I want to update the name = "Main Course" field, what is the best way? How can i send only name to the backend? how can i make parameters optional? and how can i spread the name field to replace inside my api?
This is my main.mo code
public func updateDishtype (dishType : Types.DishType) : async Result.Result<Types.DishType, Types.Error> {
let existingDishType = dishTypeRepository.getDishType(dishType.dishTypeId);
switch(existingDishType) {
case null {
#err(#NotFound);
};
case (? v) {
dishTypeRepository.upsertDishtype(dishType.dishTypeId, dishType);
#ok(dishType);
};
};
};
This is the DishTypeRepository code
public class DishTypeRepository() {
let dishTypesHashMap = HashMap.HashMap<Types.DishTypeId, Types.DishType>(0, isDishTypeIdEq, Hash.hash);
public func upsertDishtype (dishTypeId : Types.DishTypeId, dishType : Types.DishType) {
dishTypesHashMap.put(dishTypeId, dishType);
};
public func getDishType(dishTypeId : Types.DishTypeId): ?Types.DishType {
return dishTypesHashMap.get(dishTypeId);
};
}
Thanks! As a quick heads-up I’ll just point to Release 0.7.0 · dfinity/motoko · GitHub, which contains the feature (and more :-). dfx will naturally lag a bit in picking this up, but if you are of the brave kind and want to give this a try, then feel free to download the binary. (And report back. Remember that .0 caveats apply!) We’ll do some broader messaging when it is readily available through the usual channels.
Nothing nefarious, but the features are right out of the press and may contain minor bugs. We decided to jump from 0.6.30 to 0.7.0, so that people pay attention and file bug reports!