#Parameter destructuring with an interface.

8 messages · Page 1 of 1 (latest)

narrow peak
#

How do destructure a parameter with a named interface in typescript. Rn I have a compiler error telling me "cannot find name customerNo"

 async fillNewCustomerForm({ props }: { props: CustomerFormData }) {
    this.fieldCustomerNum.fill(customerNo);
  }

interface CustomerFormData {
  customerNo: string;
  name: string;
  address: string;
  city: string;
  provState: string;
  postalZip: string;
  email: string;
  website: string;
  phone: string;
  ext: string;
  fax: string;
  faxExt: string;
  contactName: string;
  contactEmail: string;
  contactPhone: string;
  typeCredit: 'No Credit' | 'Unlimited Credit' | 'Limited Credit';
  creditLimit: string;
  boolFinCharge: boolean;
  typeInvoice: InvStateOptions;
  typeStatement: InvStateOptions;
  bankInstitution: string;
  bankTransit: string;
  bankAccount: string;
  discount: string;
  boolNumPO: boolean;
  boolServiceCharge: boolean;
  boolInvLevy: boolean;
  boolWebUpload: boolean;
  userType: string;
  misc: string;
  specialCode: string;
  ref: string;
}
pure copper
#

{ props: { customerNo } }: { props: CustomerFormData }

narrow peak
copper wadi
#

JS is designed to be statically analysible, so there's no magic. If you want to access a named prop you've got to go write that name somewhere so the code can be analysed by lexers/parsers

#

Technically it's possible to make it automatic with with(props) but that is prohibited in modern JS.

narrow peak
copper wadi
#

TS isn't involved with runtime code. It's just types.

#

Anything that actually has to run at runtime is pure JavaScript, nothing will ever happen at runtime based on types - types get deleted when you compile.