Skip to content

Use function that doesn't return unknown type #54713

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

Merged
merged 2 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10165,11 +10165,20 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return prop ? getTypeOfSymbol(prop) : undefined;
}

function getTypeOfPropertyOrIndexSignature(type: Type, name: __String, addOptionalityToIndex: boolean): Type {
function getTypeOfPropertyOrIndexSignature(type: Type, name: __String): Type {
return getTypeOfPropertyOfType(type, name) || getApplicableIndexInfoForName(type, name)?.type || unknownType;
}

/**
* Similar to `getTypeOfPropertyOrIndexSignature`,
* but returns `undefined` if there is no matching property or index signature,
* and adds optionality to index signature types.
*/
function getTypeOfPropertyOrIndexSignatureOfType(type: Type, name: __String): Type | undefined {
let propType;
return getTypeOfPropertyOfType(type, name) ||
(propType = getApplicableIndexInfoForName(type, name)?.type) && addOptionality(propType, /*isProperty*/ true, addOptionalityToIndex) ||
unknownType;
(propType = getApplicableIndexInfoForName(type, name)?.type) &&
addOptionality(propType, /*isProperty*/ true, /*isOptional*/ true);
}

function isTypeAny(type: Type | undefined) {
Expand Down Expand Up @@ -22703,7 +22712,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
let matched = false;
for (let i = 0; i < types.length; i++) {
if (include[i]) {
const targetType = getTypeOfPropertyOrIndexSignature(types[i], propertyName, /*addOptionalityToIndex*/ true);
const targetType = getTypeOfPropertyOrIndexSignatureOfType(types[i], propertyName);
if (targetType && related(getDiscriminatingType(), targetType)) {
matched = true;
}
Expand Down Expand Up @@ -27016,7 +27025,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
propType = removeNullable && optionalChain ? getOptionalType(propType) : propType;
const narrowedPropType = narrowType(propType);
return filterType(type, t => {
const discriminantType = getTypeOfPropertyOrIndexSignature(t, propName, /*addOptionalityToIndex*/ false);
const discriminantType = getTypeOfPropertyOrIndexSignature(t, propName);
return !(discriminantType.flags & TypeFlags.Never) && !(narrowedPropType.flags & TypeFlags.Never) && areTypesComparable(narrowedPropType, discriminantType);
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
discriminateWithMissingProperty.ts(12,5): error TS2345: Argument of type '{ mode: "numeric"; data: Uint8Array; }' is not assignable to parameter of type 'Arg'.
Types of property 'data' are incompatible.
Type 'Uint8Array' is not assignable to type 'number'.


==== discriminateWithMissingProperty.ts (1 errors) ====
type Arg = {
mode: "numeric",
data: number,
} | {
mode: "alphabetic",
data: string,
} | {
data: string | Uint8Array;
}

declare function foo(arg: Arg): void;
foo({ mode: "numeric", data: new Uint8Array([30]) }); // Should error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '{ mode: "numeric"; data: Uint8Array; }' is not assignable to parameter of type 'Arg'.
!!! error TS2345: Types of property 'data' are incompatible.
!!! error TS2345: Type 'Uint8Array' is not assignable to type 'number'.
36 changes: 36 additions & 0 deletions tests/baselines/reference/discriminateWithMissingProperty.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//// [tests/cases/compiler/discriminateWithMissingProperty.ts] ////

=== discriminateWithMissingProperty.ts ===
type Arg = {
>Arg : Symbol(Arg, Decl(discriminateWithMissingProperty.ts, 0, 0))

mode: "numeric",
>mode : Symbol(mode, Decl(discriminateWithMissingProperty.ts, 0, 12))

data: number,
>data : Symbol(data, Decl(discriminateWithMissingProperty.ts, 1, 20))

} | {
mode: "alphabetic",
>mode : Symbol(mode, Decl(discriminateWithMissingProperty.ts, 3, 5))

data: string,
>data : Symbol(data, Decl(discriminateWithMissingProperty.ts, 4, 23))

} | {
data: string | Uint8Array;
>data : Symbol(data, Decl(discriminateWithMissingProperty.ts, 6, 5))
>Uint8Array : Symbol(Uint8Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
}

declare function foo(arg: Arg): void;
>foo : Symbol(foo, Decl(discriminateWithMissingProperty.ts, 8, 1))
>arg : Symbol(arg, Decl(discriminateWithMissingProperty.ts, 10, 21))
>Arg : Symbol(Arg, Decl(discriminateWithMissingProperty.ts, 0, 0))

foo({ mode: "numeric", data: new Uint8Array([30]) }); // Should error
>foo : Symbol(foo, Decl(discriminateWithMissingProperty.ts, 8, 1))
>mode : Symbol(mode, Decl(discriminateWithMissingProperty.ts, 11, 5))
>data : Symbol(data, Decl(discriminateWithMissingProperty.ts, 11, 22))
>Uint8Array : Symbol(Uint8Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

40 changes: 40 additions & 0 deletions tests/baselines/reference/discriminateWithMissingProperty.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//// [tests/cases/compiler/discriminateWithMissingProperty.ts] ////

=== discriminateWithMissingProperty.ts ===
type Arg = {
>Arg : { mode: "numeric"; data: number; } | { mode: "alphabetic"; data: string; } | { data: string | Uint8Array; }

mode: "numeric",
>mode : "numeric"

data: number,
>data : number

} | {
mode: "alphabetic",
>mode : "alphabetic"

data: string,
>data : string

} | {
data: string | Uint8Array;
>data : string | Uint8Array
}

declare function foo(arg: Arg): void;
>foo : (arg: Arg) => void
>arg : Arg

foo({ mode: "numeric", data: new Uint8Array([30]) }); // Should error
>foo({ mode: "numeric", data: new Uint8Array([30]) }) : void
>foo : (arg: Arg) => void
>{ mode: "numeric", data: new Uint8Array([30]) } : { mode: "numeric"; data: Uint8Array; }
>mode : "numeric"
>"numeric" : "numeric"
>data : Uint8Array
>new Uint8Array([30]) : Uint8Array
>Uint8Array : Uint8ArrayConstructor
>[30] : number[]
>30 : 30

15 changes: 15 additions & 0 deletions tests/cases/compiler/discriminateWithMissingProperty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @strict: true
// @noEmit: true

type Arg = {
mode: "numeric",
data: number,
} | {
mode: "alphabetic",
data: string,
} | {
data: string | Uint8Array;
}

declare function foo(arg: Arg): void;
foo({ mode: "numeric", data: new Uint8Array([30]) }); // Should error