Skip to content

Commit

Permalink
Per-file strictNullChecks support
Browse files Browse the repository at this point in the history
  • Loading branch information
weswigham committed Aug 24, 2022
1 parent 913e68b commit f5134a5
Show file tree
Hide file tree
Showing 29 changed files with 729 additions and 436 deletions.
641 changes: 378 additions & 263 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4576,9 +4576,9 @@ namespace ts {
/* @internal */ getParameterType(signature: Signature, parameterIndex: number): Type;
/* @internal */ getParameterIdentifierNameAtPosition(signature: Signature, parameterIndex: number): [parameterName: __String, isRestParameter: boolean] | undefined;
getNullableType(type: Type, flags: TypeFlags): Type;
getNonNullableType(type: Type): Type;
getNonNullableType(type: Type, context?: Node | undefined): Type;
/* @internal */ getNonOptionalType(type: Type): Type;
/* @internal */ isNullableType(type: Type): boolean;
/* @internal */ isNullableType(type: Type, context?: Node | undefined): boolean;
getTypeArguments(type: TypeReference): readonly Type[];

// TODO: GH#18217 `xToDeclaration` calls are frequently asserted as defined.
Expand Down Expand Up @@ -4710,8 +4710,8 @@ namespace ts {
/* @internal */ getFalseType(fresh?: boolean): Type;
/* @internal */ getTrueType(fresh?: boolean): Type;
/* @internal */ getVoidType(): Type;
/* @internal */ getUndefinedType(): Type;
/* @internal */ getNullType(): Type;
/* @internal */ getUndefinedType(widening?: boolean): Type;
/* @internal */ getNullType(widening?: boolean): Type;
/* @internal */ getESSymbolType(): Type;
/* @internal */ getNeverType(): Type;
/* @internal */ getOptionalType(): Type;
Expand Down Expand Up @@ -5606,12 +5606,12 @@ namespace ts {
Narrowable = Any | Unknown | StructuredOrInstantiable | StringLike | NumberLike | BigIntLike | BooleanLike | ESSymbol | UniqueESSymbol | NonPrimitive,
// The following flags are aggregated during union and intersection type construction
/* @internal */
IncludesMask = Any | Unknown | Primitive | Never | Object | Union | Intersection | NonPrimitive | TemplateLiteral,
IncludesMask = Any | Unknown | Primitive | Never | Object | Union | Intersection | NonPrimitive | TemplateLiteral | Index | StringMapping,
// The following flags are used for different purposes during union and intersection type construction
/* @internal */
IncludesMissingType = TypeParameter,
/* @internal */
IncludesNonWideningType = Index,
IncludesNonWideningType = 1 << 29, // repurpose for true typeflag when needed
/* @internal */
IncludesWildcard = IndexedAccess,
/* @internal */
Expand Down
92 changes: 68 additions & 24 deletions src/services/codefixes/inferFromUsage.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2323,7 +2323,7 @@ declare namespace ts {
getWidenedType(type: Type): Type;
getReturnTypeOfSignature(signature: Signature): Type;
getNullableType(type: Type, flags: TypeFlags): Type;
getNonNullableType(type: Type): Type;
getNonNullableType(type: Type, context?: Node | undefined): Type;
getTypeArguments(type: TypeReference): readonly Type[];
/** Note that the resulting nodes cannot be checked. */
typeToTypeNode(type: Type, enclosingDeclaration: Node | undefined, flags: NodeBuilderFlags | undefined): TypeNode | undefined;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2323,7 +2323,7 @@ declare namespace ts {
getWidenedType(type: Type): Type;
getReturnTypeOfSignature(signature: Signature): Type;
getNullableType(type: Type, flags: TypeFlags): Type;
getNonNullableType(type: Type): Type;
getNonNullableType(type: Type, context?: Node | undefined): Type;
getTypeArguments(type: TypeReference): readonly Type[];
/** Note that the resulting nodes cannot be checked. */
typeToTypeNode(type: Type, enclosingDeclaration: Node | undefined, flags: NodeBuilderFlags | undefined): TypeNode | undefined;
Expand Down

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/baselines/reference/collectionPatternNoError.types
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function fetchMsg<V extends Message>(protoCtor: MsgConstructor<V>): V {
>protoCtor : MsgConstructor<V>

return null!;
>null! : null
>null! : never
>null : null
}

Expand Down
1 change: 0 additions & 1 deletion tests/baselines/reference/conditionalTypes2.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ tests/cases/conformance/types/conditional/conditionalTypes2.ts(75,12): error TS2
!!! error TS2345: Property 'bat' is missing in type 'Bar & Foo' but required in type '{ foo: string; bat: string; }'.
!!! error TS2345: Type 'Extract<T, Bar>' is not assignable to type '{ foo: string; bat: string; }'.
!!! error TS2345: Property 'bat' is missing in type 'Bar & Foo' but required in type '{ foo: string; bat: string; }'.
!!! related TS2728 tests/cases/conformance/types/conditional/conditionalTypes2.ts:62:43: 'bat' is declared here.
!!! related TS2728 tests/cases/conformance/types/conditional/conditionalTypes2.ts:62:43: 'bat' is declared here.
fooBat(y); // Error
~
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function fn<T extends { x: Map<T['x']> }>(sliceIndex: T): AllArg<T['x']> {
>sliceIndex : T

return null!;
>null! : null
>null! : never
>null : null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class HelloWorld {
handleEvent3(event: C3): T1 { return undefined! } // Ok, Error
>handleEvent3 : (event: C3) => T1
>event : C3
>undefined! : undefined
>undefined! : never
>undefined : undefined
}

Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class HelloWorld {
handleEvent3(event: C3): T1 { return undefined! } // Ok, Error
>handleEvent3 : (event: C3) => T1
>event : C3
>undefined! : undefined
>undefined! : never
>undefined : undefined
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ tests/cases/compiler/genericConditionalConstrainedToUnknownNotAssignableToConcre
Type 'unknown' is not assignable to type 'A'.
Type 'ReturnType<FunctionsObj<T>[string]>' is not assignable to type 'A'.
Type 'unknown' is not assignable to type 'A'.
Property 'x' is missing in type '{}' but required in type 'A'.
Type 'unknown' is not assignable to type 'A'.


==== tests/cases/compiler/genericConditionalConstrainedToUnknownNotAssignableToConcreteObject.ts (1 errors) ====
Expand Down Expand Up @@ -38,8 +38,7 @@ tests/cases/compiler/genericConditionalConstrainedToUnknownNotAssignableToConcre
!!! error TS2322: Type 'unknown' is not assignable to type 'A'.
!!! error TS2322: Type 'ReturnType<FunctionsObj<T>[string]>' is not assignable to type 'A'.
!!! error TS2322: Type 'unknown' is not assignable to type 'A'.
!!! error TS2322: Property 'x' is missing in type '{}' but required in type 'A'.
!!! related TS2728 tests/cases/compiler/genericConditionalConstrainedToUnknownNotAssignableToConcreteObject.ts:1:15: 'x' is declared here.
!!! error TS2322: Type 'unknown' is not assignable to type 'A'.
}

// Original CFA report of the above issue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function union2<T extends object, U extends string | number>(thing: T | U) {
"key" in thing; // Ok
>"key" in thing : boolean
>"key" : "key"
>thing : T
>thing : T | (T & null) | (U & null)
}
}

Expand Down Expand Up @@ -146,7 +146,7 @@ function union5<T extends object | string, U extends object | number>(p: T | U)
"key" in p;
>"key" in p : boolean
>"key" : "key"
>p : (T & object) | (U & object)
>p : (T & object) | (U & object) | (T & null) | (U & null)
}
}

Expand Down
20 changes: 10 additions & 10 deletions tests/baselines/reference/intersectionReduction.types
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ type N1 = 'a' & 'b';
>N1 : never

type N2 = { a: string } & null;
>N2 : null
>N2 : never
>a : string
>null : null

type N3 = { a: string } & undefined;
>N3 : undefined
>N3 : never
>a : string

type N4 = string & number;
Expand Down Expand Up @@ -255,10 +255,10 @@ declare let s2: string & Tag2;
>s2 : never

declare let t1: string & Tag1 | undefined;
>t1 : undefined
>t1 : never

declare let t2: string & Tag2 | undefined;
>t2 : undefined
>t2 : never

s1 = s2;
>s1 = s2 : never
Expand All @@ -271,14 +271,14 @@ s2 = s1;
>s1 : never

t1 = t2;
>t1 = t2 : undefined
>t1 : undefined
>t2 : undefined
>t1 = t2 : never
>t1 : never
>t2 : never

t2 = t1;
>t2 = t1 : undefined
>t2 : undefined
>t1 : undefined
>t2 = t1 : never
>t2 : never
>t1 : never

// Repro from #36736

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Table<S extends Schema> {
>getRows : <C extends keyof S>() => Array<UnrollOnHover<Pick<S, C>>>

return null!
>null! : null
>null! : never
>null : null
}
}
Expand Down
14 changes: 14 additions & 0 deletions tests/baselines/reference/mappedTypeRelationships.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(40,5): error TS2
tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(41,5): error TS2322: Type 'T[keyof T]' is not assignable to type 'U[keyof T] | undefined'.
Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'U[keyof T] | undefined'.
Type 'T[string]' is not assignable to type 'U[keyof T] | undefined'.
Type 'T[string]' is not assignable to type 'U[keyof T]'.
Type 'T' is not assignable to type 'U'.
'U' could be instantiated with an arbitrary type which could be unrelated to 'T'.
tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(45,5): error TS2322: Type 'U[K] | undefined' is not assignable to type 'T[K]'.
Type 'undefined' is not assignable to type 'T[K]'.
tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(46,5): error TS2322: Type 'T[K]' is not assignable to type 'U[K] | undefined'.
Type 'T[keyof T]' is not assignable to type 'U[K] | undefined'.
Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'U[K] | undefined'.
Type 'T[string]' is not assignable to type 'U[K] | undefined'.
Type 'T[string]' is not assignable to type 'U[K]'.
Type 'T' is not assignable to type 'U'.
'U' could be instantiated with an arbitrary type which could be unrelated to 'T'.
tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(51,5): error TS2542: Index signature in type 'Readonly<T>' only permits reading.
tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(56,5): error TS2542: Index signature in type 'Readonly<T>' only permits reading.
tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(61,5): error TS2322: Type 'T[keyof T]' is not assignable to type 'U[keyof T]'.
Expand Down Expand Up @@ -146,6 +152,10 @@ tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(168,5): error TS
!!! error TS2322: Type 'T[keyof T]' is not assignable to type 'U[keyof T] | undefined'.
!!! error TS2322: Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'U[keyof T] | undefined'.
!!! error TS2322: Type 'T[string]' is not assignable to type 'U[keyof T] | undefined'.
!!! error TS2322: Type 'T[string]' is not assignable to type 'U[keyof T]'.
!!! error TS2322: Type 'T' is not assignable to type 'U'.
!!! error TS2322: 'U' could be instantiated with an arbitrary type which could be unrelated to 'T'.
!!! related TS2208 tests/cases/conformance/types/mapped/mappedTypeRelationships.ts:39:14: This type parameter might need an `extends U` constraint.
}

function f13<T, U extends T, K extends keyof T>(x: T, y: Partial<U>, k: K) {
Expand All @@ -159,6 +169,10 @@ tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(168,5): error TS
!!! error TS2322: Type 'T[keyof T]' is not assignable to type 'U[K] | undefined'.
!!! error TS2322: Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'U[K] | undefined'.
!!! error TS2322: Type 'T[string]' is not assignable to type 'U[K] | undefined'.
!!! error TS2322: Type 'T[string]' is not assignable to type 'U[K]'.
!!! error TS2322: Type 'T' is not assignable to type 'U'.
!!! error TS2322: 'U' could be instantiated with an arbitrary type which could be unrelated to 'T'.
!!! related TS2208 tests/cases/conformance/types/mapped/mappedTypeRelationships.ts:44:14: This type parameter might need an `extends U` constraint.
}

function f20<T>(x: T, y: Readonly<T>, k: keyof T) {
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/metadataOfUnionWithNull.types
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class B {
>PropDeco : (target: Object, propKey: string | symbol) => void

d: undefined | null;
>d : null
>d : never
>null : null

@PropDeco
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/noImplicitAnyForIn.types
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ for (var a in x) {
>b : any

var c = a || b;
>c : string
>a || b : string
>c : string | undefined
>a || b : string | undefined
>a : string
>b : undefined
}
Expand Down
16 changes: 8 additions & 8 deletions tests/baselines/reference/nonNullableReductionNonStrict.types
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function test<T>(f1: Transform1<T>, f2: Transform2<T>) {

f1?.("hello");
>f1?.("hello") : T
>f1 : (value: string) => T
>f1 : ((value: string) => T) | undefined
>"hello" : "hello"

f2?.("hello");
Expand All @@ -28,23 +28,23 @@ function test<T>(f1: Transform1<T>, f2: Transform2<T>) {

function f1<T>(x: T | (string extends T ? null | undefined : never)) {
>f1 : <T>(x: T | (string extends T ? null | undefined : never)) => void
>x : T | (string extends T ? null : never)
>x : T | (string extends T ? never : never)
>null : null

let z = x!; // NonNullable<T>
>z : T | (string extends T ? null : never)
>x! : T | (string extends T ? null : never)
>x : T | (string extends T ? null : never)
>z : T
>x! : T
>x : T | (string extends T ? never : never)
}

function f2<T, U extends null | undefined>(x: T | U) {
>f2 : <T, U extends null>(x: T | U) => void
>f2 : <T, U extends never>(x: T | U) => void
>null : null
>x : T | U

let z = x!; // NonNullable<T>
>z : T | U
>x! : T | U
>z : T
>x! : T
>x : T | U
}

Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ declare const a7: unknown | null
>null : null

declare const a8: never | null
>a8 : null
>a8 : never
>null : null

declare const a9: any | null
Expand Down Expand Up @@ -81,9 +81,9 @@ const aa7 = a7 ?? 'whatever'
>'whatever' : "whatever"

const aa8 = a8 ?? 'whatever'
>aa8 : "whatever"
>a8 ?? 'whatever' : "whatever"
>a8 : null
>aa8 : never
>a8 ?? 'whatever' : never
>a8 : never
>'whatever' : "whatever"

const aa9 = a9 ?? 'whatever'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tests/cases/compiler/privateFieldAssignabilityFromUnknown.ts(2,3): error TS18028: Private identifiers are only available when targeting ECMAScript 2015 and higher.
tests/cases/compiler/privateFieldAssignabilityFromUnknown.ts(5,7): error TS2741: Property '#field' is missing in type '{}' but required in type 'Class'.
tests/cases/compiler/privateFieldAssignabilityFromUnknown.ts(5,7): error TS2322: Type 'unknown' is not assignable to type 'Class'.


==== tests/cases/compiler/privateFieldAssignabilityFromUnknown.ts (2 errors) ====
Expand All @@ -11,6 +11,5 @@ tests/cases/compiler/privateFieldAssignabilityFromUnknown.ts(5,7): error TS2741:

const task: Class = {} as unknown;
~~~~
!!! error TS2741: Property '#field' is missing in type '{}' but required in type 'Class'.
!!! related TS2728 tests/cases/compiler/privateFieldAssignabilityFromUnknown.ts:2:3: '#field' is declared here.
!!! error TS2322: Type 'unknown' is not assignable to type 'Class'.

Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ var r3: string = a().toString();
var r3b: string = a()['toString']();
>r3b : Symbol(r3b, Decl(propertyAccessOnTypeParameterWithoutConstraints.ts, 21, 3))
>a : Symbol(a, Decl(propertyAccessOnTypeParameterWithoutConstraints.ts, 17, 3))
>'toString' : Symbol(Object.toString, Decl(lib.es5.d.ts, --, --))

var b = {
>b : Symbol(b, Decl(propertyAccessOnTypeParameterWithoutConstraints.ts, 23, 3))
Expand Down
Loading

0 comments on commit f5134a5

Please sign in to comment.