Code is:
const foo = (foo: string) => {
const result = []
result.push(foo)
}
I get the following TS error:
[t
You need to type result
to an array of string const result: string[] = [];
.
All you have to do is define your result
as a string array, like the following:
const result : string[] = [];
Without defining the array type, it by default will be never
. So when you tried to add a string to it, it was a type mismatch, and so it threw the error you saw.
Remove "strictNullChecks": true from "compilerOptions" or set it to false in the tsconfig.json file of your Ng app. These errors will go away like anything and your app would compile successfully.
Disclaimer: This is just a workaround. This error appears only when the null checks are not handled properly which in any case is not a good way to get things done.
I got the same error in ReactJS function component, using ReactJS useState hook. The solution was to declare the type of useState at initialisation:
const [items , setItems] = useState<IItem[]>([]); // replace IItem[] with your own typing: string, boolean...
The solution i found was
const [files, setFiles] = useState([] as any);
I was able to get past this by using the Array keyword instead of empty brackets:
const enhancers: Array<any> = [];
Use:
if (typeof devToolsExtension === 'function') {
enhancers.push(devToolsExtension())
}