Why is eslint ignoring my strictNullChecks setting? - reactjs

I am trying to port a large existing JavaScript codebase to TypeScript. I have installed typescript, #typescript-eslint/eslint-plugin and #typescript-eslint/parser as devDependencies and configured the project so that tsconfig.json is used.
My (minimal) tsconfig.json is:
{
"compilerOptions": {
"target": "es5",
"module": "esnext",
"alwaysStrict": true,
"strict": true,
"strictNullChecks": true
},
}
Running eslint on an existing .tsx file (correctly) shows some errors:
$ ./node_modules/.bin/eslint ./src/components/JustSomeComponent.tsx
/src/components/JustSomeComponent.tsx
57:75 error Unexpected empty arrow function 'myFunction' #typescript-eslint/no-empty-function
...
✖ 20 problems (8 errors, 12 warnings)
4 errors and 0 warnings potentially fixable with the `--fix` option.
However, I wanted to make sure that strictNullChecks setting was indeed enabled, so I created a new file and made a mistake on purpose:
import React from 'react';
export default class MyComponent extends React.Component {
render(): React.ReactNode {
const x: number = null; // I would expect an error here!
console.log(x);
return null;
}
}
But running the check on this file returns no errors:
$ ./node_modules/.bin/eslint ./src/components/MyComponent.tsx
$
Removing the return type for render() method results in an error, so the checks are performed - I am just not warned about invalid null assignements.
Any idea how I can enable strictNullChecks, or at least how to debug why it is disabled?

strictNullChecks is a TypeScript feature, not an ESLint feature.

As #jonrsharpe commented, strictNullChecks is a compiler setting and does not affect eslint.
The reason I wasn't getting any error was that eslint found errors in other files and prevented build from starting. Once I fixed those, build step indeed failed with an error as expected:
$ npm run build
> myapp#0.0.1 build
> react-scripts build
Creating an optimized production build...
Failed to compile.
/src/components/MyComponent.tsx
TypeScript error in /src/components/MyComponent.tsx(5,11):
Type 'null' is not assignable to type 'number'. TS2322
3 | export default class MyComponent extends React.Component {
4 | render(): React.ReactNode {
> 5 | const x: number = null;
| ^
6 | console.log(x);
7 | return null;
8 | }

Related

nextjs v12 swcMinify set to true

decided to get rid of my babelrc as the new rust compiler is being promoted and encouraged by vercel. however doing so immediately breaks my build. this is the only thing in my babelrc.
{
"presets": ["next/babel"],
"plugins": [["styled-components", { "ssr": true, "displayName": true }]]
}
and this is the error after i add
module.exports = {
swcMinify: true
to my next.config.js and i run next build
not sure what i'm missing here since development build works fine, just the build that seems to be breaking on compile. any help would be appreciated
Edit: as requested, this is the entire error
static/chunks/pages/_app-434406ef89d5b351.js from Terser
error: 'eval' and 'arguments' cannot be used as a binding identifier in strict mode
|
97440 | function eval(str) {
| ^^^^
Caused by:
0: failed to parse input file
1: error was recoverable, but proceeding would result in wrong codegen
2: Syntax Error
Error: error: 'eval' and 'arguments' cannot be used as a binding identifier in strict mode
|
97440 | function eval(str) {
| ^^^^
Caused by:
0: failed to parse input file
1: error was recoverable, but proceeding would result in wrong codegen
2: Syntax Error
Create a new file and name it .babelrc (if it does not exist) and add following content:
{
"presets": ["next/babel"]
}
Also open next.config.js and add swcMinify:false

Error: Automatic publicPath is not supported in this browser when i am running my test cases and every thing is fine

Why my test cases are not running and my component is rendering fine?
Test suite failed to run
Automatic publicPath is not supported in this browser
1 | import React from 'react';
> 2 | import { getInputStyleClassName } from '#app-
utility/stylesManager';
| ^
I had the same problem. For me, in my style manager package, I had to put the following in my webpack.config.js.
module.exports = {
output: {
publicPath: '',
...
},
};
You can find more info on this here: https://github.com/webpack-contrib/mini-css-extract-plugin/issues/707
this error is coming because of a silly mistake.
I forgot to mention the public path field in my style manager package.

TypeScript skipLibCheck still checking node_modules libs

I am using TypeScript with React, and TypeScript is still checking libraries in node_modules folder, although I have "skipLibCheck" set to true in tsconfig.json..
Here's my tsconfig.json (I added the exclude section for troubleshooting, which also didn't work):
{
"compilerOptions": {
"target": "es5",
"forceConsistentCasingInFileNames": true,
"module": "commonjs",
"jsx": "react",
"declaration": true,
"sourceMap": true,
"experimentalDecorators": true,
"skipLibCheck": true,
"typeRoots": [
"./node_modules/#types"
],
"types": [
"es6-promise",
"webpack-env"
],
"lib": [
"es5",
"dom",
"es2015.collection"
]
},
"exclude": [
"node_modules",
"./node_modules",
"./node_modules/*",
"./node_modules/#types/node/index.d.ts",
]
}
React version am using is 15.4.2, and TypeScript is installed globally... I had version 3.7.2, and I upgraded it to 3.7.3 because I read somewhere that skipLibCheck doesn't work with 3.7.2 ..
The error I am getting when trying to build the project with gulp is:
Error - typescript - node_modules\gsap\types\gsap-utils.d.ts(97,75): error TS1144: '{' or ';' expected
If I set skipLibCheck to false, and build the project, I'll have MANY more errors. So seems like the skipLibcheck works partially.
Any idea how to solve this? I am still new to TypeScript. Any help would be appreciated.
skipLibCheck is not meant to prevent all type checking in node_modules. Although it may work for some projects, but it's just a coincidence. You could say it works partially, true. Here's what it does:
Skip Lib Check - skipLibCheck
Skip type checking of declaration files.
This can save time during compilation at the expense of type-system
accuracy. For example, two libraries could define two copies of the
same type in an inconsistent way. Rather than doing a full check of
all d.ts files, TypeScript will type check the code you specifically
refer to in your app’s source code.
A common case where you might think to use skipLibCheck is when there
are two copies of a library’s types in your node_modules. In these
cases, you should consider using a feature like yarn’s resolutions to
ensure there is only one copy of that dependency in your tree or
investigate how to ensure there is only one copy by understanding the
dependency resolution to fix the issue without additional tooling.
skipLibCheck was introduced in Typescipt 2.0, so upgrading Typescript isn't really a fix. Yet again it may work for some people.
Now I had a case when I had to add a library using Typescript 4 to a project using Typescript 3. It was raining errors on build. Having the same version of typescript helped. The version of typescript would be specific to your project here.
The only quick solution I know is to use require instead of import (my project was backend):
import * as lib from 'lib';
const lib = require('lib');
skipLib can only skip .d.ts errors. But if you use .ts files directly from node_modules, tsc can not igonre the type errors. You may try add "// #ts-noCheck" before every .ts file in node_modules.
Here's my node script for adding // #ts-ignore before every .ts、*tsx file in node_modules. You can run it before run tsc.
// 将 node_modules 下面的每个 ts,tsx 文件头部都加上 // #ts-noCheck,以忽略 node_modules 下面的类型错误
const fs = require('fs');
const path = require('path');
const traversalPath = `./node_modules`;
function folderTraveral(filePath) {
// 根据文件路径读取文件,返回文件列表
fs.readdir(filePath, function (err, files) {
if (err) {
console.warn(err);
} else {
// 遍历读取到的文件列表
files.forEach(function (filename) {
// 获取当前文件的绝对路径
const filedir = path.join(filePath, filename);
// 根据文件路径获取文件信息,返回一个fs.Stats对象
fs.stat(filedir, function (eror, stats) {
if (eror) {
console.warn('获取文件stats失败');
} else {
const isFile = stats.isFile(); // 是文件
const isDir = stats.isDirectory(); // 是文件夹
if (
isFile &&
!filedir.endsWith('d.ts') &&
(filedir.endsWith('ts') || filedir.endsWith('tsx'))
) {
let content = fs.readFileSync(filedir, 'utf-8');
if (!content.startsWith('// #ts-nocheck')) {
content = '// #ts-nocheck \n' + content;
fs.writeFileSync(filedir, content, 'utf-8');
}
}
if (isDir) {
folderTraveral(filedir); // 递归,如果是文件夹,就继续遍历该文件夹下面的文件
}
}
});
});
}
});
}
folderTraveral(traversalPath);

Gatsby fail to compile with flow

I have installed Flow in my Gatsby project, by adding the plugin gatsby-plugin-flow.
It has created a .flowconfig file in project root and a "gatsby-plugin-flow": "^1.0.4" dependency in package.json.
When I run gatsby develop, there is an error :
ERROR in ./src/components/layout.js
Module build failed (from ./node_modules/gatsby/dist/utils/babel-loader.js):
SyntaxError: /Users/iqc/project/src/components/layout.js: Unexpected token, expected "," (10:29)
8 | import "./layout.css"
9 |
> 10 | const Layout = ({ children } : Object) => (
| ^
11 | <StaticQuery
12 | query={graphql`
It looks like Babel is failing to compile the project.
Maybe a problem with ES6 in Gatsby ?
I also tried to install Flow with official docs (no plugin), but the same problem occurs.
Thanks !
You have installed gatsby-plugin-flow, but you also need to add it as a plugin in the gatsby-config.js file.
gatsby-config.js
module.exports = {
plugins: ['gatsby-plugin-flow'],
}

Angular 2 compiled ahead of time gives error "A platform with a different configuration has been created."

I'm trying to figure out this whole compile Ahead-of-Time idea. I'm sold on the benefits but the process of actually doing it is giving me a headache.
I'm following the cookbook as close as possible. Probably the only big differences is that their main.ts is my boot.ts but my boot.ts is literally cut and paste of their main.ts. I have everything they say to install and the following tasks in my package.json:
"ngc": "ngc -p tsconfig-aot.json",
"rollup": "rollup -c rollup-config.js",
"copy-dist-files": "node build-scripts/copy-dist-files.js",
"aot": "npm run ngc && npm run rollup && npm run copy-dist-files"
tsconfig-aot.json:
{
"compilerOptions": {
"target": "es5",
"module": "es2015",
"moduleResolution": "node",
"inlineSourceMap": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"pretty": false
},
"angularCompilerOptions": {
"genDir": "aot",
"skipMetadataEmit" : true
}
}
rollup-config.js:
import rollup from 'rollup';
import nodeResolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import uglify from 'rollup-plugin-uglify';
export default {
entry: 'aot/app/boot.js',
dest: 'aot/app/bundle.js', // output a single application bundle
moduleName: 'aot', // no idea what I'm SUPPOSED to put here, the rollup complained without it though
sourceMap: false,
format: 'iife',
plugins: [
nodeResolve({jsnext: true, module: true}),
commonjs({
include: 'node_modules/rxjs/**',
}),
uglify()
]
}
My copy-dist-files.js is just a node script to move my css and images into place in aot/ and seems to be working just fine across all platforms. Honestly nothing about that file is really angular specific. If you need it I'll post it but I'm trying to keep things clean.
After I run npm run aot it completes successfully and I run http-server aot to serve up the folder and I get this error in the console:
Uncaught Error: A platform with a different configuration has been created. Please destroy it first.
An error I've never received using JiT compilation. I have no idea where this came from. Everytime I google it I find people asking about it relating to testing but this is about the bundle running in-browser. Those questions skirt around the actual error text because the issue is with their testing scaffolding. I don't think I'm loading any other platform and I don't know how to destory it. What's especially odd is that the site seems to run and function completely normally.
Not uglifying the bundle.js doesn't help because the error seems to be thrown inside Angular's innards in portions I didn't write and would rather not edit.
I'm running Angular 2.1.1 and the error is showing in all browsers. What's going on here?
I figured it out and sadly because I didn't understand the process I was working on I didn't give enough information in the question.
I was defining my module AND bootstrapping it in the same file (boot.ts) so when I followed the cookbook and created a boot-aot.ts that pointed to my old boot.ts there were still references and calls to #angular/platform-browser-dynamic (specific to JIT) and then there were calls to #angular/platform-browser (specific to AOT). This is how there were two platforms created.
The fix was to define my module in module.ts and have two ultra simple boots:
boot.ts:
import { platformBrowserDynamic } from '#angular/platform-browser-dynamic';
import { AppModule } from './module';
platformBrowserDynamic().bootstrapModule(AppModule)
.catch((err: any) => console.error(err));
and boot-aot.ts:
import { platformBrowser } from '#angular/platform-browser';
import { AppModuleNgFactory } from '../aot/app/module.ngfactory';
platformBrowser().bootstrapModuleFactory(AppModuleNgFactory)
.catch((err: any) => console.error(err));
boot.ts will be used for JIT compiling during debugging and boot-aot.ts is referenced in the rollup-config.js.

Resources