Skip to main content

การ Import

  • ใช้การย่อ path เสมอ เพื่อลดความยากในการอ่าน
  • ถ้าใช้การ auto import กรุณาตรวจสอบความถูกต้องทุกครั้ง
    import { formatNumeric } from '@/functions';
    
    /// หรือ ถ้ามี type ด้วย
    import { type FormatNumericFunc, formatNumeric } from '@/functions';
    

รูปแบบ

  • ใช้ arrow function และ กำหนด parameter ในรูปแบบ object เสมอ เพื่อให้โค้ดไปในทิศทางเดียวกัน และง่ายต่อการเรียกใช้งาน
  • ใส่ return type เมื่อจำเป็น (ใส่ตลอดได้ยิ่งดี)
    ทำ
    const sampleFunc = () => { ... }
    
    const sampleFunc = ({ firstParam, secondParam, ... }: SampleFuncParams) => {
      ...
      return '';
    }
    
    const sampleFunc = ({ firstParam, secondParam, ... }: SampleFuncParams): string => {
      ...
      return '';
    }
    
    ไม่ทำ
    function sampleFunc() { ... }
    
    function sampleFunc(firstParam: string, secondParam: string) {
      ...
      return '';
    }
    
    const sampleFunc = (firstParam: string, secondParam: string): string => {
      ...
      return '';
    }
    

ตัวอย่าง Example

  • ประกาศ
    const formatNumeric = ({ value, minFractionDigit = 0, maxFractionDigit = 0 }: FormatNumericInput): string => {
      ...
    }
    
  • กำหนด Type
    interface FormatNumericInput {
      value?: number | null;
      minFractionDigit?: number;
      maxFractionDigit?: number;
    }
    
  • ใช้งาน
    const formatted = formatNumeric({ value: 1234.56, minFractionDigit: 2, maxFractionDigit: 4 });