TypeScript类模型对应JSON对象缺失属性问题咨询
Hey there! Let's work through the missing property problems you're seeing with your MarketingPhase object relative to your TypeScript class definitions. First, let's recap your code to make sure we're aligned.
Your Class Definitions
export class Period { lifeYears: number; lifeDevelopment: number; } export class PairYearQuantity { id: number; year: number; quantity: number; } export class MarketingPhase { status: string; period: Period; investments: PairYearQuantity[]; costs: PairYearQuantity[]; }
Your Incomplete MarketingPhase Object
const marketingPhase: MarketingPhase = { status: 'empty', period: { lifeYears: 3, lifeDevelopment: 2 }, investments: [ { id: 1, year: 2018, quantity: 15 }, { id: 2, year: 2... // Incomplete entry here ], // Missing the `costs` property entirely! };
Key Issues Identified
Looking at your code, there are two clear missing property problems:
- Incomplete
investmentsarray entry: The second item in yourinvestmentsarray cuts off mid-definition (year: 2...), so it's missing a fullyearvalue and the requiredquantityproperty. - Missing
costsproperty: YourMarketingPhaseclass explicitly defines acostsproperty of typePairYearQuantity[], but your object doesn't include it at all. TypeScript will throw an error here because all class properties are required by default.
Fixes & Best Practices
Here's how to resolve these issues, plus tips to avoid similar problems down the line:
1. Complete All Required Properties
First, finish the incomplete investments entry and add the costs property (even an empty array works as a placeholder if you don't have data yet). Here's the corrected object:
const marketingPhase: MarketingPhase = { status: 'empty', period: { lifeYears: 3, lifeDevelopment: 2 }, investments: [ { id: 1, year: 2018, quantity: 15 }, { id: 2, year: 2019, quantity: 20 } // Completed entry ], costs: [] // Added required property };
2. Mark Properties as Optional (If Needed)
If you don't always need certain properties (like costs), you can mark them as optional with the ? syntax. This tells TypeScript the property doesn't have to be present:
export class MarketingPhase { status: string; period: Period; investments: PairYearQuantity[]; costs?: PairYearQuantity[]; // Now optional }
3. Use Class Instantiation (For More Control)
Instead of object literals, you can instantiate classes with the new keyword—this is great if you want to add default values via a constructor:
export class MarketingPhase { status: string; period: Period; investments: PairYearQuantity[]; costs: PairYearQuantity[]; // Constructor with default values constructor( status: string = 'empty', period: Period = { lifeYears: 0, lifeDevelopment: 0 }, investments: PairYearQuantity[] = [], costs: PairYearQuantity[] = [] ) { this.status = status; this.period = period; this.investments = investments; this.costs = costs; } } // Create an instance easily (missing properties use defaults) const marketingPhase = new MarketingPhase('empty', { lifeYears: 3, lifeDevelopment: 2 }, [ { id: 1, year: 2018, quantity: 15 }, { id: 2, year: 2019, quantity: 20 } ]); // `costs` automatically uses the empty array default
4. Enable Strict Type-Checking
Make sure strict: true is turned on in your tsconfig.json—this catches missing property errors early, helping you avoid these issues during development.
内容的提问来源于stack exchange,提问作者igogra

