Skip to content

Commit a560fd6

Browse files
committed
Change the default type parameter constraint and default to unknown from {}
1 parent b7881a2 commit a560fd6

File tree

128 files changed

+1238
-849
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

128 files changed

+1238
-849
lines changed

src/compiler/checker.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7325,7 +7325,7 @@ namespace ts {
73257325
const declaredType = <MappedType>getTypeFromMappedTypeNode(type.declaration);
73267326
const constraint = getConstraintTypeFromMappedType(declaredType);
73277327
const extendedConstraint = constraint && constraint.flags & TypeFlags.TypeParameter ? getConstraintOfTypeParameter(<TypeParameter>constraint) : constraint;
7328-
type.modifiersType = extendedConstraint && extendedConstraint.flags & TypeFlags.Index ? instantiateType((<IndexType>extendedConstraint).type, type.mapper || identityMapper) : emptyObjectType;
7328+
type.modifiersType = extendedConstraint && extendedConstraint.flags & TypeFlags.Index ? instantiateType((<IndexType>extendedConstraint).type, type.mapper || identityMapper) : unknownType;
73297329
}
73307330
}
73317331
return type.modifiersType;
@@ -7768,7 +7768,7 @@ namespace ts {
77687768
* type itself. Note that the apparent type of a union type is the union type itself.
77697769
*/
77707770
function getApparentType(type: Type): Type {
7771-
const t = type.flags & TypeFlags.Instantiable ? getBaseConstraintOfType(type) || emptyObjectType : type;
7771+
const t = type.flags & TypeFlags.Instantiable ? getBaseConstraintOfType(type) || (strictNullChecks ? unknownType : emptyObjectType) : type;
77727772
return getObjectFlags(t) & ObjectFlags.Mapped ? getApparentTypeOfMappedType(<MappedType>t) :
77737773
t.flags & TypeFlags.Intersection ? getApparentTypeOfIntersectionType(<IntersectionType>t) :
77747774
t.flags & TypeFlags.StringLike ? globalStringType :
@@ -8101,7 +8101,7 @@ namespace ts {
81018101
const baseDefaultType = getDefaultTypeArgumentType(isJavaScriptImplicitAny);
81028102
for (let i = numTypeArguments; i < numTypeParameters; i++) {
81038103
let defaultType = getDefaultFromTypeParameter(typeParameters![i]);
8104-
if (isJavaScriptImplicitAny && defaultType && isTypeIdenticalTo(defaultType, emptyObjectType)) {
8104+
if (isJavaScriptImplicitAny && defaultType && (isTypeIdenticalTo(defaultType, unknownType) || isTypeIdenticalTo(defaultType, emptyObjectType))) {
81058105
defaultType = anyType;
81068106
}
81078107
result[i] = defaultType ? instantiateType(defaultType, createTypeMapper(typeParameters!, result)) : baseDefaultType;
@@ -8480,7 +8480,7 @@ namespace ts {
84808480
const typeParameters = signature.typeParameters;
84818481
if (typeParameters) {
84828482
const typeEraser = createTypeEraser(typeParameters);
8483-
const baseConstraints = map(typeParameters, tp => instantiateType(getBaseConstraintOfType(tp), typeEraser) || emptyObjectType);
8483+
const baseConstraints = map(typeParameters, tp => instantiateType(getBaseConstraintOfType(tp), typeEraser) || unknownType);
84848484
return instantiateSignature(signature, createTypeMapper(typeParameters, baseConstraints), /*eraseTypeParameters*/ true);
84858485
}
84868486
return signature;
@@ -10794,7 +10794,7 @@ namespace ts {
1079410794
* This is used during inference when instantiating type parameter defaults.
1079510795
*/
1079610796
function createBackreferenceMapper(context: InferenceContext, index: number): TypeMapper {
10797-
return t => findIndex(context.inferences, info => info.typeParameter === t) >= index ? emptyObjectType : t;
10797+
return t => findIndex(context.inferences, info => info.typeParameter === t) >= index ? unknownType : t;
1079810798
}
1079910799

1080010800
function combineTypeMappers(mapper1: TypeMapper | undefined, mapper2: TypeMapper): TypeMapper;
@@ -11341,7 +11341,7 @@ namespace ts {
1134111341
function isTypeDerivedFrom(source: Type, target: Type): boolean {
1134211342
return source.flags & TypeFlags.Union ? every((<UnionType>source).types, t => isTypeDerivedFrom(t, target)) :
1134311343
target.flags & TypeFlags.Union ? some((<UnionType>target).types, t => isTypeDerivedFrom(source, t)) :
11344-
source.flags & TypeFlags.InstantiableNonPrimitive ? isTypeDerivedFrom(getBaseConstraintOfType(source) || emptyObjectType, target) :
11344+
source.flags & TypeFlags.InstantiableNonPrimitive ? isTypeDerivedFrom(getBaseConstraintOfType(source) || unknownType, target) :
1134511345
target === globalObjectType ? !!(source.flags & (TypeFlags.Object | TypeFlags.NonPrimitive)) :
1134611346
target === globalFunctionType ? !!(source.flags & TypeFlags.Object) && isFunctionObjectType(source as ObjectType) :
1134711347
hasBaseType(source, getTargetType(target));
@@ -14539,7 +14539,7 @@ namespace ts {
1453914539
function getTypeFromInference(inference: InferenceInfo) {
1454014540
return inference.candidates ? getUnionType(inference.candidates, UnionReduction.Subtype) :
1454114541
inference.contraCandidates ? getIntersectionType(inference.contraCandidates) :
14542-
emptyObjectType;
14542+
unknownType;
1454314543
}
1454414544

1454514545
function inferTypes(inferences: InferenceInfo[], originalSource: Type, originalTarget: Type, priority: InferencePriority = 0, contravariant = false) {
@@ -15097,7 +15097,7 @@ namespace ts {
1509715097
}
1509815098

1509915099
function getDefaultTypeArgumentType(isInJavaScriptFile: boolean): Type {
15100-
return isInJavaScriptFile ? anyType : emptyObjectType;
15100+
return isInJavaScriptFile ? anyType : unknownType;
1510115101
}
1510215102

1510315103
function getInferredTypes(context: InferenceContext): Type[] {
@@ -15441,7 +15441,7 @@ namespace ts {
1544115441
return strictNullChecks ? TypeFacts.ObjectStrictFacts : TypeFacts.ObjectFacts;
1544215442
}
1544315443
if (flags & TypeFlags.Instantiable) {
15444-
return getTypeFacts(getBaseConstraintOfType(type) || emptyObjectType);
15444+
return getTypeFacts(getBaseConstraintOfType(type) || unknownType);
1544515445
}
1544615446
if (flags & TypeFlags.UnionOrIntersection) {
1544715447
return getTypeFactsOfTypes((<UnionOrIntersectionType>type).types);
@@ -16752,7 +16752,7 @@ namespace ts {
1675216752
}
1675316753

1675416754
function typeHasNullableConstraint(type: Type) {
16755-
return type.flags & TypeFlags.InstantiableNonPrimitive && maybeTypeOfKind(getBaseConstraintOfType(type) || emptyObjectType, TypeFlags.Nullable);
16755+
return type.flags & TypeFlags.InstantiableNonPrimitive && maybeTypeOfKind(getBaseConstraintOfType(type) || unknownType, TypeFlags.Nullable);
1675616756
}
1675716757

1675816758
function getConstraintForLocation(type: Type, node: Node): Type;
@@ -18194,7 +18194,7 @@ namespace ts {
1819418194
}
1819518195

1819618196
function getJsxPropsTypeFromCallSignature(sig: Signature, context: JsxOpeningLikeElement) {
18197-
let propsType = getTypeOfFirstParameterOfSignatureWithFallback(sig, emptyObjectType);
18197+
let propsType = getTypeOfFirstParameterOfSignatureWithFallback(sig, unknownType);
1819818198
propsType = getJsxManagedAttributesFromLocatedAttributes(context, getJsxNamespaceAt(context), propsType);
1819918199
const intrinsicAttribs = getJsxType(JsxNames.IntrinsicAttributes, context);
1820018200
if (intrinsicAttribs !== errorType) {
@@ -18268,7 +18268,7 @@ namespace ts {
1826818268
const forcedLookupLocation = getJsxElementPropertiesName(ns);
1826918269
let attributesType = forcedLookupLocation === undefined
1827018270
// If there is no type ElementAttributesProperty, return the type of the first parameter of the signature, which should be the props type
18271-
? getTypeOfFirstParameterOfSignatureWithFallback(sig, emptyObjectType)
18271+
? getTypeOfFirstParameterOfSignatureWithFallback(sig, unknownType)
1827218272
: forcedLookupLocation === ""
1827318273
// If there is no e.g. 'props' member in ElementAttributesProperty, use the element class type instead
1827418274
? getReturnTypeOfSignature(sig)
@@ -18280,7 +18280,7 @@ namespace ts {
1828018280
if (!!forcedLookupLocation && !!length(context.attributes.properties)) {
1828118281
error(context, Diagnostics.JSX_element_class_does_not_support_attributes_because_it_does_not_have_a_0_property, unescapeLeadingUnderscores(forcedLookupLocation));
1828218282
}
18283-
return emptyObjectType;
18283+
return unknownType;
1828418284
}
1828518285

1828618286
attributesType = getJsxManagedAttributesFromLocatedAttributes(context, ns, attributesType);
@@ -21986,7 +21986,7 @@ namespace ts {
2198621986
const decl = parameter.valueDeclaration as ParameterDeclaration;
2198721987
if (decl.name.kind !== SyntaxKind.Identifier) {
2198821988
// if inference didn't come up with anything but {}, fall back to the binding pattern if present.
21989-
if (links.type === emptyObjectType) {
21989+
if (links.type === unknownType) {
2199021990
links.type = getTypeFromBindingPattern(decl.name);
2199121991
}
2199221992
assignBindingElementTypes(decl.name);
@@ -21999,28 +21999,28 @@ namespace ts {
2199921999
const globalPromiseType = getGlobalPromiseType(/*reportErrors*/ true);
2200022000
if (globalPromiseType !== emptyGenericType) {
2200122001
// if the promised type is itself a promise, get the underlying type; otherwise, fallback to the promised type
22002-
promisedType = getAwaitedType(promisedType) || emptyObjectType;
22002+
promisedType = getAwaitedType(promisedType) || unknownType;
2200322003
return createTypeReference(globalPromiseType, [promisedType]);
2200422004
}
2200522005

22006-
return emptyObjectType;
22006+
return unknownType;
2200722007
}
2200822008

2200922009
function createPromiseLikeType(promisedType: Type): Type {
2201022010
// creates a `PromiseLike<T>` type where `T` is the promisedType argument
2201122011
const globalPromiseLikeType = getGlobalPromiseLikeType(/*reportErrors*/ true);
2201222012
if (globalPromiseLikeType !== emptyGenericType) {
2201322013
// if the promised type is itself a promise, get the underlying type; otherwise, fallback to the promised type
22014-
promisedType = getAwaitedType(promisedType) || emptyObjectType;
22014+
promisedType = getAwaitedType(promisedType) || unknownType;
2201522015
return createTypeReference(globalPromiseLikeType, [promisedType]);
2201622016
}
2201722017

22018-
return emptyObjectType;
22018+
return unknownType;
2201922019
}
2202022020

2202122021
function createPromiseReturnType(func: FunctionLikeDeclaration | ImportCall, promisedType: Type) {
2202222022
const promiseType = createPromiseType(promisedType);
22023-
if (promiseType === emptyObjectType) {
22023+
if (promiseType === unknownType) {
2202422024
error(func, isImportCall(func) ?
2202522025
Diagnostics.A_dynamic_import_call_returns_a_Promise_Make_sure_you_have_a_declaration_for_Promise_or_include_ES2015_in_your_lib_option :
2202622026
Diagnostics.An_async_function_or_method_must_return_a_Promise_Make_sure_you_have_a_declaration_for_Promise_or_include_ES2015_in_your_lib_option);
@@ -23453,7 +23453,7 @@ namespace ts {
2345323453
// If the contextual type is a type variable constrained to a primitive type, consider
2345423454
// this a literal context for literals of that primitive type. For example, given a
2345523455
// type parameter 'T extends string', infer string literal types for T.
23456-
const constraint = getBaseConstraintOfType(contextualType) || emptyObjectType;
23456+
const constraint = getBaseConstraintOfType(contextualType) || unknownType;
2345723457
return maybeTypeOfKind(constraint, TypeFlags.String) && maybeTypeOfKind(candidateType, TypeFlags.StringLiteral) ||
2345823458
maybeTypeOfKind(constraint, TypeFlags.Number) && maybeTypeOfKind(candidateType, TypeFlags.NumberLiteral) ||
2345923459
maybeTypeOfKind(constraint, TypeFlags.BigInt) && maybeTypeOfKind(candidateType, TypeFlags.BigIntLiteral) ||

tests/baselines/reference/callGenericFunctionWithIncorrectNumberOfTypeArguments.types

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,24 +136,24 @@ class C2<T, U> {
136136
}
137137
}
138138
var r6 = (new C2()).f<number>(1, '');
139-
>r6 : {}
140-
>(new C2()).f<number>(1, '') : {}
141-
>(new C2()).f : (x: {}, y: {}) => {}
142-
>(new C2()) : C2<{}, {}>
143-
>new C2() : C2<{}, {}>
139+
>r6 : unknown
140+
>(new C2()).f<number>(1, '') : unknown
141+
>(new C2()).f : (x: unknown, y: unknown) => unknown
142+
>(new C2()) : C2<unknown, unknown>
143+
>new C2() : C2<unknown, unknown>
144144
>C2 : typeof C2
145-
>f : (x: {}, y: {}) => {}
145+
>f : (x: unknown, y: unknown) => unknown
146146
>1 : 1
147147
>'' : ""
148148

149149
var r6b = (new C2()).f<number, string, number>(1, '');
150-
>r6b : {}
151-
>(new C2()).f<number, string, number>(1, '') : {}
152-
>(new C2()).f : (x: {}, y: {}) => {}
153-
>(new C2()) : C2<{}, {}>
154-
>new C2() : C2<{}, {}>
150+
>r6b : unknown
151+
>(new C2()).f<number, string, number>(1, '') : unknown
152+
>(new C2()).f : (x: unknown, y: unknown) => unknown
153+
>(new C2()) : C2<unknown, unknown>
154+
>new C2() : C2<unknown, unknown>
155155
>C2 : typeof C2
156-
>f : (x: {}, y: {}) => {}
156+
>f : (x: unknown, y: unknown) => unknown
157157
>1 : 1
158158
>'' : ""
159159

tests/baselines/reference/callGenericFunctionWithZeroTypeArguments.types

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ class C2<T> {
8383
}
8484
}
8585
var r6 = (new C2()).f(1);
86-
>r6 : {}
87-
>(new C2()).f(1) : {}
88-
>(new C2()).f : (x: {}) => {}
89-
>(new C2()) : C2<{}>
90-
>new C2() : C2<{}>
86+
>r6 : unknown
87+
>(new C2()).f(1) : unknown
88+
>(new C2()).f : (x: unknown) => unknown
89+
>(new C2()) : C2<unknown>
90+
>new C2() : C2<unknown>
9191
>C2 : typeof C2
92-
>f : (x: {}) => {}
92+
>f : (x: unknown) => unknown
9393
>1 : 1
9494

9595
interface I2<T> {

0 commit comments

Comments
 (0)