ts-node icon indicating copy to clipboard operation
ts-node copied to clipboard

error TS2339: Property 'find' does not exist on type 'ImageState[]'

Open ShinShil opened this issue 8 years ago • 3 comments

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.

ShinShil avatar Aug 19 '18 10:08 ShinShil

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.

blakeembrey avatar Aug 19 '18 15:08 blakeembrey

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'.

SimonSDA avatar Nov 26 '18 10:11 SimonSDA

Adding "lib": ["esnext"] solved it in my case.

tormodhau avatar Mar 25 '20 08:03 tormodhau