You cannot declare parameters of a member function
as public. That is only valid for constructors where it is used as a shorthand for:
- declaring a member variable
- also assigning the input parameter to the member variable
Question
I write code for valid phone number. But when I compile project I have following mistakes:
output from command ""C:\Program Files (x86)\Microsoft SDKs\TypeScript\tsc" "C:..\Scripts\MyScripts\TS\BuildPhone.ts" "C:..\Scripts\MyScripts\TS\ProductsViewModel.ts" "C:..\Scripts\MyScripts\TS\helloWorld.ts"" with code 1.
// Interface
interface IPhoneBuild
{
showPhone(): string;
checkPhone(): boolean;
}
class Phone
{
Code: string;
Number: string;
Fax: boolean;
}
// Module
module Phones
{
// Class
export class PhoneBuild implements IPhoneBuild
{
private phone: Phone;
private codes: string[];
// Constructor
constructor(public Number: string, public Code: string, public Codes: string[])
{
this.phone = this.buildPhone(Number, Code);
this.codes = Codes;
}
//Private Methods
private clearPhone(public reg: string)
{
var re = /\W/g;
return reg.replace(re, "");
}
private buildPhone(public num: string, public code: string)
{
var p: Phone = { Code: "", Number: "", Fax: false };
num = this.clearPhone(num);
if (num.length == 6)
{
p = { Code: code, Fax: false, Number: num };
}
if (num.length == 11)
{
p = { Code: num.substring(0, 4), Fax: false, Number: num.substring(4)};
}
return p;
}
// Instance member
public showPhone()
{
return this.phone.Code + this.phone.Number;
}
public checkPhone()
{
return this.phone.Number.length != 7 || this.phone.Code.length == 0;
}
}
}
What do you think about? I need help.
I found resolve: Delete "public" in properties in methods: buildPhone and clearPhone.
Solution
You cannot declare parameters of a member function
as public. That is only valid for constructors where it is used as a shorthand for:
OTHER TIPS
I have given your code a quick tidy.
Where you use public
or private
in a constructor, you can avoid the mapping from the parameter to a property on the class as the TypeScript compiler will do it automatically for you.
Your method parameters don't need these access modifiers - they can't be public
or private
as they are scoped to the function (i.e. they only live for the duration of the function and cannot be accessed outside of the function).
The example below should compile fine.
// Interface
interface IPhoneBuild
{
showPhone(): string;
checkPhone(): boolean;
}
class Phone
{
Code: string;
Number: string;
Fax: boolean;
}
// Module
module Phones
{
// Class
export class PhoneBuild implements IPhoneBuild
{
private phone: Phone;
// Constructor
constructor(phoneNumber: string, code: string, public codes: string[])
{
this.phone = this.buildPhone(phoneNumber, code);
}
//Private Methods
private clearPhone(reg: string)
{
var re = /\W/g;
return reg.replace(re, "");
}
private buildPhone(num: string, code: string)
{
var phone: Phone = { Code: "", Number: "", Fax: false };
num = this.clearPhone(num);
if (num.length == 6)
{
phone.Code = code;
phone.Number = num;
}
if (num.length == 11)
{
phone.Code = num.substring(0, 4);
phone.Number = num.substring(4);
}
return phone;
}
// Instance member
public showPhone()
{
return this.phone.Code + this.phone.Number;
}
public checkPhone()
{
return this.phone.Number.length != 7 || this.phone.Code.length == 0;
}
}
}