2

I have an Angular/TypeScript application that uses Shippable CI/CD. Everything works great normally but now getting a TS error on Shippable, but not when building locally. (same angular, node, and TS versions both locally and shippable)

The error is

ERROR in src/search.component.ts(81,23): error TS2352: Type 'DataGroup' cannot be converted to type 'DataGroup[]'. Property 'includes' is missing in type 'DataGroup'. src/search.component.ts(83,27): error TS2352: Type 'DataGroup' cannot be converted to type 'DataGroup[]'.

The offending lines are

dataGroups = _.chain(dataGroups)
  .filter(dataGroup => !metaDataGroupNames.includes(dataGroup.spreadsheetId))
  .sortBy('spreadsheetId')
  .value();
metaDataGroups = _.chain(dataGroups)
  .filter(dataGroup => metaDataGroupNames.includes(dataGroup.spreadsheetId))
  .sortBy('spreadsheetId')
  .value();

In the above, dataGroups and metaDataGroups are type DataGroup[]... and filter+sort does indeed return the same.

dataGroups: Array<DataGroup>;
metaDataGroups: Array<DataGroup>;

I tried to cast the result from value with ...value() as DataGroup[]; to no avail.

I also tried to throw everything on a single line like the below incase that was the issue, also to no avail.

this.dataGroups = _.chain(dataGroups).filter(dataGroup => !metaDataGroupNames.includes(dataGroup.spreadsheetId)).sortBy('spreadsheetId').value() as DataGroup[];

this.metaDataGroups = _.chain(dataGroups).filter(dataGroup => metaDataGroupNames.includes(dataGroup.spreadsheetId)).sortBy('spreadsheetId').value() as DataGroup[];

update/workaround

So I never figured out the actual issue but I just broke out the underscore operations into their own statements and the problem went away. Still weird how I was getting the TS error on shippable but not locally, and that it wasn't an error in the first place.

this.dataGroups = _.filter(dataGroups, dataGroup => !metaDataGroupNames.includes(dataGroup.spreadsheetId));
this.dataGroups = _.sortBy(dataGroups, 'spreadsheetId');
this.metaDataGroups = _.filter(dataGroups, dataGroup => metaDataGroupNames.includes(dataGroup.spreadsheetId));
this.metaDataGroups = _.sortBy(dataGroups, 'spreadsheetId');
4

1 回答 1

1

嗨,也许你可以使用这样的东西,我改变了实现some使用includes

const metaDataGroupNames = [ { spreadsheetId:1, name:'one' }, { spreadsheetId:2, name:'two'}];

const dataGroups = [{spreadsheetId:1, name:'one'},{spreadsheetId:3,name:'three'}];

const filtered = dataGroups.filter(item => metaDataGroupNames.some(y => y.spreadsheetId == item.spreadsheetId));

console.log(filtered);

于 2019-09-08T01:56:26.127 回答