2025年6月

react17, typescript에서
export interface LinesData{
lineId: string;
lineName: string;
cudsKey: string;
_checked: boolean;
}
정의하고

selectLineData: LinesData = []; 했는데

Type 'never[]' is missing 오류 해결해 주세요

에러 메시지 Type 'never[]' is not assignable to type 'LinesData'는 selectLineData를 배열로 초기화했는데, 타입을 단일 객체(LinesData)로 선언했기 때문입니다.

당신의 코드:

ts
복사
편집
export interface LinesData {
lineId: string;
lineName: string;
cudsKey: string;
_checked: boolean;
}

selectLineData: LinesData = []; // ❌ 오류 발생
이건 LinesData 타입에 배열을 대입했기 때문에 타입 불일치로 에러가 발생합니다.

✅ 해결 방법: 타입을 배열로 선언
selectLineData가 여러 개의 라인을 저장하는 배열이라면 다음과 같이 타입을 수정해야 합니다:

ts
복사
편집
selectLineData: LinesData[] = []; // ✅ 배열 타입으로 선언
참고: 단일 객체일 경우
만약 selectLineData가 단일 LinesData 객체라면, 빈 배열이 아니라 객체로 초기화해야 합니다:

ts
복사
편집
selectLineData: LinesData = {
lineId: '',
lineName: '',
cudsKey: '',
_checked: false
};

React, Typescript의 클래스 컴퍼넌트 환경에서 monitoringType 변수가 T 일 경우 'Activity Name'를 리턴하고 S 일 경우
'Monitoring Name'를 리턴하는 예제 만들어 주세요.

import React, { Component } from 'react';

type MonitoringType = 'T' | 'S';

interface Props {
monitoringType: MonitoringType;
}

class MonitoringLabel extends Component {
getLabel = (): string => {

const { monitoringType } = this.props;

switch (monitoringType) {
  case 'T':
    return 'Activity Name';
  case 'S':
    return 'Monitoring Name';
  default:
    return 'Unknown';
}

};

render() {

return (
  <div>
    <label>{this.getLabel()}</label>
  </div>
);

}
}

export default MonitoringLabel;

switch 문장 말고 간단하게 T이면 Activity Name 나머지는 Monitoring Name 으로 간략하게 수정해주세요

import React, { Component } from 'react';

type MonitoringType = 'T' | 'S';

interface Props {
monitoringType: MonitoringType;
}

class MonitoringLabel extends Component {
getLabel = (): string => {

return this.props.monitoringType === 'T' ? 'Activity Name' : 'Monitoring Name';

};

render() {

return (
  <div>
    <label>{this.getLabel()}</label>
  </div>
);

}
}

export default MonitoringLabel;