Skip to content
← Back to rules

typescript/no-unnecessary-template-expression 의심스러운

💭 This rule requires type information.
An auto-fix is available for this rule.

수행하는 작업

필요 없는 템플릿 표현식(삽입)을 허용하지 않습니다. 이러한 표현식은 단순화할 수 있습니다.

왜 나쁜가요?

불필요한 치환 표현식을 포함하는 템플릿 리터럴은 추가적인 복잡성을 더하며, 어떤 이점도 제공하지 않습니다. 정적 문자열 리터럴이나 이미 문자열인 표현식은 간단히 할 수 있습니다.

참고: 이 규칙은 치환 표현식이 없는 템플릿 리터럴에는 적용되지 않습니다. 예를 들어, `안녕`'안녕'으로 작성할 수 있지만 허용됩니다.

예시

이 규칙에 부적절한 코드 예시:

ts
// 정적 값은 주변 템플릿에 통합될 수 있음
const ab1 = `${"a"}${"b"}`;
const ab2 = `a${"b"}`;

const stringWithNumber = `${"1 + 1 = "}${2}`;
const stringWithBoolean = `${"true는 "}${true}`;

// 이미 문자열인 표현식은 템플릿 없이 다시 작성할 수 있음
const text = "a";
const wrappedText = `${text}`;

declare const intersectionWithString: string & { _brand: "test-brand" };
const wrappedIntersection = `${intersectionWithString}`;

이 규칙에 적절한 코드 예시:

ts
// 템플릿에 통합된 정적 값
const ab1 = `ab`;

// 복잡하지 않은 삽입이 있는 템플릿
const name = "세계";
const greeting = `안녕하세요 ${name}!`;

// 표현식이 포함된 템플릿
const result = `결과: ${1 + 2}`;

// 간단한 문자열은 템플릿이 필요 없음
const text = "a";
const wrappedText = text;

// 다중 줄 문자열은 문제가 되지 않음
const multiline = `
  안녕하세요
  세계
`;

사용 방법

구성 파일 또는 명령줄 인터페이스에서 이 규칙을 활성화하려면 다음을 사용할 수 있습니다:

json
{
  "rules": {
    "typescript/no-unnecessary-template-expression": "error"
  }
}
bash
oxlint --type-aware --deny typescript/no-unnecessary-template-expression

참고 자료