4 ms·
> In TypeScript 2.0, null and undefined have their own types which allows developers to explicitly express when null/undefined values are acceptable. Now, when
by sdegutis 10y ago
> In TypeScript 2.0, null and undefined have their own types which allows developers to explicitly express when null/undefined values are acceptable. Now, when something can be either a number or null, you can describe it with the union type number | null (which reads as “number or null”).
Great news, but I suspect it's going to be pretty difficult to migrate large codebases to account for this properly, even with the help of `--strictNullChecks`. Sounds like days worth of tedious work analyzing every function.
- klodolph 10y agoYes, although the same can be said for the "no implicit any" flag when migrating from code bases EcmaScript. They seem to be doing a pretty good job of supporting both legacy and greenfield projects.
- k__ 10y agoYes, I tried "no implicit any" and got nothing working. Problem were mainly 3rd party libs with out-dated, bad or no type definitions. Is it even possible to use these flags with production code?
- RyanCavanaugh 10y agoWe've removed nearly all the implicit anys from DefinitelyTyped, so if you're getting definitions from there, you shouldn't see any. We don't have good data on this but my perception from talking to people on GitHub is that the vast majority of people using TypeScript run with 'no implicit any' on once they get their codebases converted. (I'm on the TypeScript team)
- k__ 10y agoNice to read :) Had some problems with React/JSX back in the days.
- johnny_reilly 10y agoAnd even if noImplicitAny in 3rd party libraries is not honoured you now have: skipLibCheck which will limit checks to your own code. Details here: https://www.typescriptlang.org/docs/handbook/compiler-options.html https://www.typescriptlang.org/docs/handbook/compiler-option... I believe it's actually a perf gain to run with this set (but I'm not certain).
- jahewson 10y agoThat behaviour is only enabled if `--strictNullChecks` is set to true. Otherwise, by default all types include null/undefined just like in TypeScript 1.x. So it's entirely opt-in.
- ken47 10y agoAgreed. But worth it, at the end of the day, wouldn't you say?
- evmar 10y agoAt my work we migrated our codebase (a few hundred klines at the time, I think) with not too much effort. It helps that enabling the flag makes everything more restrictive, so the compiler only flags the places that you need to go back and add the |null bits.