TypeStat icon indicating copy to clipboard operation
TypeStat copied to clipboard

SNC missing first property in PropertyAccessExpression

Open markjm opened this issue 4 years ago • 2 comments
trafficstars

🐛 Bug Report

  • TypeStat version: 0.5.3
  • TypeScript version: 4.4.4
  • Node version: 16.x

Getting lazy with repro steps, as ill just add it as test. posting as text below:

type C = string | null;
type B = { C: C } | null;
type A = { B: B } | null;

const A1 = {
  B: {
    C: null
  }
} as A;

const x: string = A1.B.C;

Actual Behavior

const x: string = A1.B!.C!;

Expected Behavior

const x: string = A1!.B!.C!;
`

markjm avatar Oct 27 '21 20:10 markjm

Actually a simpler case:

type C = string | null;
const C1: C = null as C;
const y: string = C1;

does not yield any changes. Perhaps because the error from TS is presented on y as: Type 'C' is not assignable to type 'string'. Type 'null' is not assignable to type 'string'.ts(2322) (as opposed to Object is possibly 'null'.ts(2531))

Even though adding a ! after C1 fixes the issue, its maybe not "technically" a SNC issue?

markjm avatar Oct 27 '21 20:10 markjm

Aha, this is an SNC issue. There's no fixer for variable declarations -- just binary expressions!

SNC fixers that exist:

  • fixStrictNonNullAssertionBinaryExpressions
  • fixStrictNonNullAssertionCallExpressions
  • fixStrictNonNullAssertionObjectLiterals
  • fixStrictNonNullAssertionPropertyAccesses
  • fixStrictNonNullAssertionReturnTypes

If you switch the test case to a binary expression the ! gets added:

type C = string | null;
const C1: C = null as C;
let y: string;
- y = C1;
+ y = C1!;

@markjm are you feeling up for a bigger contribution to add a fixStrictNonNullAssertionVariableDeclaration? 😄 -- no worries if you don't have the time!

JoshuaKGoldberg avatar Oct 28 '21 00:10 JoshuaKGoldberg