Q) How do I use the following interface module in the service below?
If I've got the following model generated from c#:
declare module App.Core.Model {
interface Address extends App.Core.Model.BaseEntity {
addressLine1: string;
addressLine2: string;
addressLine3: string;
area: string;
countryCode: string;
name: string;
postcode: string;
town: string;
}
}
Service:
// these don't work, get "model.ts error is not a module"
import {Client} from '../interfaces/model';
import {AdminUser} from '../interfaces/model';
import {Building} from '../interfaces/model';
#Injectable()
export class AppService {
...
}
The files live at:
app/interfaces/model.ts
app/services/service.ts
You can do the following (dots have been replaced by underscore):
declare module App_Core_Model {
export interface Address extends App.Core.Model.BaseEntity {
addressLine1: string;
addressLine2: string;
addressLine3: string;
area: string;
countryCode: string;
name: string;
postcode: string;
town: string;
}
}
declare module "app_core_model" {
export = App_Core_Model;
}
Then the interfaces should be available when importing the "app_core_model" module, for instance:
import {Address} from 'app_core_model';
Also it appears that a module name cannot contain a dot because it breaks the import
Related
I'm having trouble on validating nested object when I try to update a specific prop of a nested object e.g.:
import { Type } from 'class-transformer';
import { IsOptional, IsString, ValidateNested } from 'class-validator';
class Nested {
#IsString()
#IsOptional()
readonly propA: string;
#IsString()
#IsOptional()
readonly propB: string;
}
export class UpdateDto {
#IsOptional()
#Type(() => Nested)
#ValidateNested()
readonly nested: Nested;
}
If I try to pass a dotted key like { "nested.propA": "test" } I get the following error
{
"statusCode": 400,
"message": [
"property nested.propA should not exist"
],
"error": "Bad Request"
}
A simplest solution can be adding in the parent object the dotted keys eg:
class Nested {
#IsString()
#IsOptional()
readonly propA: string;
#IsString()
#IsOptional()
readonly propB: string;
}
export class UpdateDto {
#IsOptional()
#Type(() => Nested)
#ValidateNested()
readonly nested: Nested;
#IsString()
#IsOptional()
readonly 'nested.propA': string; <==== THIS
#IsString()
#IsOptional()
readonly 'nested.propB': string; <==== THIS
}
With that approach I can pass both the entire object and a single prop, but it seems really redundant to me.
Any other way to validate using a "dot" syntax? The db is mongo where the dot syntax can be used to updating a specific prop of a nested object.
Thanks.
I need to create a model class, that have all the properties in the signup form.
but there is some properties that are not required!
how I can make this class and set those properties as not required?
export class CompanyModel {
companyName: string;
companyEmail: string;
password: string;
logo: string;
phoneNumber: string;
country: string;
city: string;
degree: string;
relatedJobs: // this is not required property//
}
I am getting the following error trying to implement an interface.
Build:Type '{ code: string; name: string; gender: string; annualSalary: number; dateOfBirth: string; }[]' is not assignable to type 'IEmployee[]'.
Here is the interface
//file employee.ts
export interface IEmployee {
code: string;
name: string;
gender: string;
annualSalary: number;
dateOfBirth: string;
//method
computeMonthlySalary(annualSalary: number): number;
}
export class Employee implements IEmployee {
constructor(public code: string, public name: string, public gender: string,
public annualSalary: number, public dateOfBirth: string) {
}
computeMonthlySalary(annualSalary: number): number {
return annualSalary / 12;
}
}
I am trying to implement it in employeelist.component.ts
//file employeelist.component.ts
import { Component } from '#angular/core';
import { IEmployee } from './employee';
#Component({
selector: 'list-employee',
templateUrl: 'app/employee/employeelist.component.html',
styleUrls: ['app/employee/employeelist.component.css']
})
export class EmployeeListComponent {
employees: IEmployee[];
// employees: any[]; //this works fine
selectedEmployeeCountRadioButton: string = 'All';
//getting error on this.employees
constructor() {
this.employees ={code:'emp101',name:'Tom',gender:'Male',annualSalary:95500,dateOfBirth:'12/6/1981'}];
}
}
Please guide!
The object you declared does not have the computeMonthlySalary method
try this
import { IEmployee, Employee } from './employee';
//...
this.employees =[new Employee('emp101','Tom','Male',95500,'12/6/1981')];
I've a class and a protocol into myModel.swift
public protocol IModelArgs{
var name: String { get set};
var surname: String { get set};
}
public class Model {
var name: String;
var surname: String;
init(args: IModelArgs) {
self.name = args.name;
self.surname = args.surname;
}
}
IModelArgs is the protocol of arguments object passed to Model constructor.
Into another file I need to create the instance of Model class, but I'm not able to pass args object to constructor: What I'm wrong?
let myM = Model(args: ("T1","T2"));
The main problem in your case that ("T1","T2") is a tuple and not the object that conform your protocol. In your case it should look like this:
struct ArgsObject: IModelArgs {
var name: String
var surname: String
}
let myM = Model(args: ArgsObject(name: "someName", surname: "someSurname"))
But if you want to use the protocol only to pass an object to the constructor, you do not need to do this. Create struct for it like this:
struct ArgsObject {
let name: String
let surname: String
}
class Model {
var name: String
var surname: String
init(args: ArgsObject) {
self.name = args.name
self.surname = args.surname
}
}
let myM = Model(args: ArgsObject(name: "someName", surname: "someSurname"))
Few optional comments
Don't use ; and protocol names like ISomething, it's not the Java
I am new to typescript. I have defined some classes. I have used them as a type for the property of another class. e.g:
fileone.ts
export class A {
propertyOne: string;
propertyTwo: string;
}
Now i have another class B in another file:
filetwo.ts
import { A } from './fileone';
export class B {
myProperty: A;
mySecondProperty: string;
}
I have instantiated this class B in another file.
I have the following code:
myapp.ts
import { B } from './filetwo';
export class C {
let myObj: B = new B();
myObj.myProperty.propertyOne = 'hello';
myObj.myProperty.propertyTwo = 'world'';
console.log(myObj);
}
Now when i try to set the property of A through B, it say the following error:
Cannot set the property "propertyOne" of undefined
Can we not do this like in java? And please explain why i cannot do what i am doing right now. And what is the correct approach for this. Please do not just give me solution to my problem but also an explanation.
You have set the correct type of your myProperty member but this variable is not initialized by just declaring the type. So you are trying to set a property propertyOne on an undefined variable on your instance of B.
If you want to have it initialized correctly, you need to do this manually in your class B:
export class B {
myProperty: A;
constructor() {
this.myProperty = new A();
}
}
myObj.myProperty is type of A which is not defined yet, so you should initialize it
myObj.myProperty = new A();
then use it
It may seem out of scope, but I had the same issues except I was using VueJs also. So, to piggy back off of Andreas Jägle answer. This minor variation worked for me except I had to add super(); to my class constructor.
importmodule.ts
import Vue from 'vue';
import { Component, Prop } from 'vue-property-decorator';
import { MyProfile } from '../../data';
interface CurrentUser {
name: string;
title: string;
groupName: string; }
#Component
export default class Profile extends Vue {
profile: MyProfile;
constructor() {
super();
this.profile = new MyProfile();
} }
data.ts
export interface IPro {
name: string;
title: string;
groupName: string;
}
export class MyProfile implements IPro {
name = "User";
title = "Title";
groupName = "Group";
}