Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TSconfig version 2 #73

Merged
merged 3 commits into from
Apr 11, 2023
Merged

TSconfig version 2 #73

merged 3 commits into from
Apr 11, 2023

Conversation

filiptammergard
Copy link
Contributor

@filiptammergard filiptammergard commented Mar 31, 2023

  • feat!: enable noUncheckedIndexedAccess rule

This ensured type safety related to accessing unknown object properties and array members.

interface Environment {
  name: string
  os: string
  [key: string]: string
}

const env: Environment = {
  name: "name",
  os: "os",
}

const { name, os, blabla } = env

// with `"noUncheckedIndexedAccess": "false"`, type of `blabla` is `string`, which makes this all good from a TypeScript perspective, but leads to runtime error:
console.log(name, os, blabla.toString())

// with `"noUncheckedIndexedAccess": "true"`, type of `blabla` is `string | undefined` which catches the potential runtime error in the type level:
console.log(name, os, blabla.toString()) // 'blabla' is possibly 'undefined'.ts(18048)
console.log(name, os, blabla?.toString()) // all good!
const foo = ["bar"]
const baz = foo[1]

// with `"noUncheckedIndexedAccess": "false"`, type of `baz` is `string` which is incorrect and can result in runtime error
// with `"noUncheckedIndexedAccess": "true"`, type of `baz` is `string | undefined` which is correct!
  • feat!: change moduleResolution to bundler

Makes TypeScript resolve files in a way similar to how Vite and other bundlers resolve files. Documentation: microsoft/TypeScript#51669 Evan You (creator of Vite) explicitly recommends using this for Vite apps: https://twitter.com/youyuxi/status/1636551895002255362

"moduleResolution": "bundler" makes module resolution respect package.json exports field. One implication of this is that you can't import from internal file structure unless it's allowed in the package's exports conditions.

// allowed in v1 with "moduleResolution": "node", but disallowed in v2
import { TextProps } from "@einride/ui/dist/components/typography/Text/Text

Disallowing this is a feature, since the internal file structure can change any time, and it's not been specified as allowed import path in package.json exports. There are often ways of solving it in other ways, as inferring types instead of importing:

// ok in v2
type TextProps = ComponentProps<typeof Text>

If something needs to be imported and can't be inferred or solve in another way, it should be officially exported instead.

Another problem is that cypress.config.ts is not being resolved correctly when switching moduleResolution to bundler. This is a known problem that Cypress is working on: cypress-io/cypress#26308 Until it's solved upstream, a possible workaround it to rename the file to cypress.config.mjs, which removes the need for Cypress to transpile the config file to JavaScript. Another workaround is to explicitly set that ts-node should be using "moduleResolution": "node": cypress-io/cypress#26308 (comment)

  • feat!: require typescript v5

Required for "moduleResolution": "bundler".

@filiptammergard filiptammergard changed the title TSconfig updates TSconfig version 2 Mar 31, 2023
@stenehall
Copy link

How easy is the upgrade process to TS5? Have you noticed any problems when upgrading?

@filiptammergard
Copy link
Contributor Author

How easy is the upgrade process to TS5? Have you noticed any problems when upgrading?

For me it has been without any required changes in all projects I have upgraded. Since TypeScript is not following SemVer, 5.0 is mostly just the version that comes after 4.9, nothing too special with the major version change.

@filiptammergard filiptammergard marked this pull request as ready for review April 5, 2023 08:47
@filiptammergard filiptammergard requested review from a team April 5, 2023 08:47
@Jontii
Copy link

Jontii commented Apr 6, 2023

@stenehall I upgraded Typescript 5 in Orch without any issues at all. It complained about a type safety issue as well, which was really good

Copy link

@Jontii Jontii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants