error TS2339: Property 'find' does not exist on type 'ImageState[]'
This is code
class ImageState {
private static constructorCallCounter = 0;
public readonly id: number;
constructor(public readonly image: string) {
this.id = ImageState.constructorCallCounter;
ImageState.constructorCallCounter++;
}
}
const images: ImageState[] = [new ImageState('test')];
console.log(images.find(imageState => imageState.image === 'test').image);
Code from above has been saved to file ts-bug.ts. When running ts-node ts-bug.ts ts-node throws an error error TS2339: Property 'find' does not exist on type 'ImageState[]'. If I change
console.log(images.find(imageState => imageState.image === 'test').image);
to
console.log((images as any).find(imageState => imageState.image === 'test').image);
code compiles and runs, and I get expected output 'test'. I think that this is bug, because typeof ImageState[] is array, and I expect array has find function.
What is your tsconfig.json? This does not look like a bug to me, the TypeScript compiler is just using a different ES version than you’re running.
Have seen numerous posts on this and the proposed answer of adding "es6" into the lib section of tsconfig.json does not work for me. Here is my tsconfig:
{
"compilerOptions": {
"module": "es2015",
"moduleResolution": "node",
"target": "es5",
"sourceMap": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"strictPropertyInitialization": false,
"skipDefaultLibCheck": true,
"skipLibCheck": true, // Workaround for https://github.com/angular/angular/issues/17863. Remove this if you upgrade to a fixed version of Angular.
"strict": true,
"lib": [ "es6", "dom", "es2017.object", "es2016.array.include" ],
"types": [ "webpack-env" ]
},
"exclude": [ "bin", "node_modules" ],
"atom": { "rewriteTsconfig": false }
}
My code:
@Input() methodParams: IParameterControlMapping;
let theParam: IParameterControlMapping | undefined = this.methodParams.find(m => m.ParameterName == parameterName);
TS2339: Property 'find' does not exist on type 'IParameterControlMapping'.
Adding "lib": ["esnext"] solved it in my case.