Skip to content

Consider as const expressions as potentially context sensitive #62221

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21140,6 +21140,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const { expression } = node as JsxExpression;
return !!expression && isContextSensitive(expression);
}
case SyntaxKind.AsExpression:
case SyntaxKind.TypeAssertionExpression:
return isConstTypeReference((node as AsExpression | TypeAssertion).type) && isContextSensitive((node as AsExpression | TypeAssertion).expression);
}

return false;
Expand Down
21 changes: 21 additions & 0 deletions tests/baselines/reference/intraExpressionInferences.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -344,4 +344,25 @@ intraExpressionInferences.ts(133,26): error TS2339: Property 'nonexistent' does
},
consumer: (val) => {},
});

// https://github.com/microsoft/TypeScript/issues/62220

declare function testAsConst<T, D>(arg: {
input: D;
produce: (arg: D) => T;
consume: (arg: T) => number;
}): D;

const testAsConstResult1 = testAsConst({
input: 100,
produce: (arg) => arg.toString(),
consume: (arg) => arg.length,
} as const);

// included for comparison with the above
const testAsConstResult2 = testAsConst({
input: 100,
produce: (arg) => arg.toString(),
consume: (arg) => arg.length,
});

39 changes: 39 additions & 0 deletions tests/baselines/reference/intraExpressionInferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,27 @@ const distantRes = distant({
},
consumer: (val) => {},
});

// https://github.com/microsoft/TypeScript/issues/62220

declare function testAsConst<T, D>(arg: {
input: D;
produce: (arg: D) => T;
consume: (arg: T) => number;
}): D;

const testAsConstResult1 = testAsConst({
input: 100,
produce: (arg) => arg.toString(),
consume: (arg) => arg.length,
} as const);

// included for comparison with the above
const testAsConstResult2 = testAsConst({
input: 100,
produce: (arg) => arg.toString(),
consume: (arg) => arg.length,
});


//// [intraExpressionInferences.js]
Expand Down Expand Up @@ -518,6 +539,17 @@ var distantRes = distant({
},
consumer: function (val) { },
});
var testAsConstResult1 = testAsConst({
input: 100,
produce: function (arg) { return arg.toString(); },
consume: function (arg) { return arg.length; },
});
// included for comparison with the above
var testAsConstResult2 = testAsConst({
input: 100,
produce: function (arg) { return arg.toString(); },
consume: function (arg) { return arg.length; },
});


//// [intraExpressionInferences.d.ts]
Expand Down Expand Up @@ -648,3 +680,10 @@ declare function distant<T>(args: {
consumer: (val: T) => unknown;
}): T;
declare const distantRes: number;
declare function testAsConst<T, D>(arg: {
input: D;
produce: (arg: D) => T;
consume: (arg: T) => number;
}): D;
declare const testAsConstResult1: 100;
declare const testAsConstResult2: number;
74 changes: 74 additions & 0 deletions tests/baselines/reference/intraExpressionInferences.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -1076,3 +1076,77 @@ const distantRes = distant({

});

// https://github.com/microsoft/TypeScript/issues/62220

declare function testAsConst<T, D>(arg: {
>testAsConst : Symbol(testAsConst, Decl(intraExpressionInferences.ts, 329, 3))
>T : Symbol(T, Decl(intraExpressionInferences.ts, 333, 29))
>D : Symbol(D, Decl(intraExpressionInferences.ts, 333, 31))
>arg : Symbol(arg, Decl(intraExpressionInferences.ts, 333, 35))

input: D;
>input : Symbol(input, Decl(intraExpressionInferences.ts, 333, 41))
>D : Symbol(D, Decl(intraExpressionInferences.ts, 333, 31))

produce: (arg: D) => T;
>produce : Symbol(produce, Decl(intraExpressionInferences.ts, 334, 11))
>arg : Symbol(arg, Decl(intraExpressionInferences.ts, 335, 12))
>D : Symbol(D, Decl(intraExpressionInferences.ts, 333, 31))
>T : Symbol(T, Decl(intraExpressionInferences.ts, 333, 29))

consume: (arg: T) => number;
>consume : Symbol(consume, Decl(intraExpressionInferences.ts, 335, 25))
>arg : Symbol(arg, Decl(intraExpressionInferences.ts, 336, 12))
>T : Symbol(T, Decl(intraExpressionInferences.ts, 333, 29))

}): D;
>D : Symbol(D, Decl(intraExpressionInferences.ts, 333, 31))

const testAsConstResult1 = testAsConst({
>testAsConstResult1 : Symbol(testAsConstResult1, Decl(intraExpressionInferences.ts, 339, 5))
>testAsConst : Symbol(testAsConst, Decl(intraExpressionInferences.ts, 329, 3))

input: 100,
>input : Symbol(input, Decl(intraExpressionInferences.ts, 339, 40))

produce: (arg) => arg.toString(),
>produce : Symbol(produce, Decl(intraExpressionInferences.ts, 340, 13))
>arg : Symbol(arg, Decl(intraExpressionInferences.ts, 341, 12))
>arg.toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --))
>arg : Symbol(arg, Decl(intraExpressionInferences.ts, 341, 12))
>toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --))

consume: (arg) => arg.length,
>consume : Symbol(consume, Decl(intraExpressionInferences.ts, 341, 35))
>arg : Symbol(arg, Decl(intraExpressionInferences.ts, 342, 12))
>arg.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
>arg : Symbol(arg, Decl(intraExpressionInferences.ts, 342, 12))
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))

} as const);
>const : Symbol(const)

// included for comparison with the above
const testAsConstResult2 = testAsConst({
>testAsConstResult2 : Symbol(testAsConstResult2, Decl(intraExpressionInferences.ts, 346, 5))
>testAsConst : Symbol(testAsConst, Decl(intraExpressionInferences.ts, 329, 3))

input: 100,
>input : Symbol(input, Decl(intraExpressionInferences.ts, 346, 40))

produce: (arg) => arg.toString(),
>produce : Symbol(produce, Decl(intraExpressionInferences.ts, 347, 13))
>arg : Symbol(arg, Decl(intraExpressionInferences.ts, 348, 12))
>arg.toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --))
>arg : Symbol(arg, Decl(intraExpressionInferences.ts, 348, 12))
>toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --))

consume: (arg) => arg.length,
>consume : Symbol(consume, Decl(intraExpressionInferences.ts, 348, 35))
>arg : Symbol(arg, Decl(intraExpressionInferences.ts, 349, 12))
>arg.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
>arg : Symbol(arg, Decl(intraExpressionInferences.ts, 349, 12))
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))

});

125 changes: 125 additions & 0 deletions tests/baselines/reference/intraExpressionInferences.types
Original file line number Diff line number Diff line change
Expand Up @@ -1743,3 +1743,128 @@ const distantRes = distant({

});

// https://github.com/microsoft/TypeScript/issues/62220

declare function testAsConst<T, D>(arg: {
>testAsConst : <T, D>(arg: { input: D; produce: (arg: D) => T; consume: (arg: T) => number; }) => D
> : ^ ^^ ^^ ^^ ^^^^^
>arg : { input: D; produce: (arg: D) => T; consume: (arg: T) => number; }
> : ^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^ ^^^

input: D;
>input : D
> : ^

produce: (arg: D) => T;
>produce : (arg: D) => T
> : ^ ^^ ^^^^^
>arg : D
> : ^

consume: (arg: T) => number;
>consume : (arg: T) => number
> : ^ ^^ ^^^^^
>arg : T
> : ^

}): D;

const testAsConstResult1 = testAsConst({
>testAsConstResult1 : 100
> : ^^^
>testAsConst({ input: 100, produce: (arg) => arg.toString(), consume: (arg) => arg.length,} as const) : 100
> : ^^^
>testAsConst : <T, D>(arg: { input: D; produce: (arg: D) => T; consume: (arg: T) => number; }) => D
> : ^ ^^ ^^ ^^ ^^^^^
>{ input: 100, produce: (arg) => arg.toString(), consume: (arg) => arg.length,} as const : { readonly input: 100; readonly produce: (arg: 100) => string; readonly consume: (arg: string) => number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^
>{ input: 100, produce: (arg) => arg.toString(), consume: (arg) => arg.length,} : { readonly input: 100; readonly produce: (arg: 100) => string; readonly consume: (arg: string) => number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^

input: 100,
>input : 100
> : ^^^
>100 : 100
> : ^^^

produce: (arg) => arg.toString(),
>produce : (arg: 100) => string
> : ^ ^^^^^^^^^^^^^^^^
>(arg) => arg.toString() : (arg: 100) => string
> : ^ ^^^^^^^^^^^^^^^^
>arg : 100
> : ^^^
>arg.toString() : string
> : ^^^^^^
>arg.toString : (radix?: number) => string
> : ^ ^^^ ^^^^^
>arg : 100
> : ^^^
>toString : (radix?: number) => string
> : ^ ^^^ ^^^^^

consume: (arg) => arg.length,
>consume : (arg: string) => number
> : ^ ^^^^^^^^^^^^^^^^^^^
>(arg) => arg.length : (arg: string) => number
> : ^ ^^^^^^^^^^^^^^^^^^^
>arg : string
> : ^^^^^^
>arg.length : number
> : ^^^^^^
>arg : string
> : ^^^^^^
>length : number
> : ^^^^^^

} as const);

// included for comparison with the above
const testAsConstResult2 = testAsConst({
>testAsConstResult2 : number
> : ^^^^^^
>testAsConst({ input: 100, produce: (arg) => arg.toString(), consume: (arg) => arg.length,}) : number
> : ^^^^^^
>testAsConst : <T, D>(arg: { input: D; produce: (arg: D) => T; consume: (arg: T) => number; }) => D
> : ^ ^^ ^^ ^^ ^^^^^
>{ input: 100, produce: (arg) => arg.toString(), consume: (arg) => arg.length,} : { input: number; produce: (arg: number) => string; consume: (arg: string) => number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^

input: 100,
>input : number
> : ^^^^^^
>100 : 100
> : ^^^

produce: (arg) => arg.toString(),
>produce : (arg: number) => string
> : ^ ^^^^^^^^^^^^^^^^^^^
>(arg) => arg.toString() : (arg: number) => string
> : ^ ^^^^^^^^^^^^^^^^^^^
>arg : number
> : ^^^^^^
>arg.toString() : string
> : ^^^^^^
>arg.toString : (radix?: number) => string
> : ^ ^^^ ^^^^^
>arg : number
> : ^^^^^^
>toString : (radix?: number) => string
> : ^ ^^^ ^^^^^

consume: (arg) => arg.length,
>consume : (arg: string) => number
> : ^ ^^^^^^^^^^^^^^^^^^^
>(arg) => arg.length : (arg: string) => number
> : ^ ^^^^^^^^^^^^^^^^^^^
>arg : string
> : ^^^^^^
>arg.length : number
> : ^^^^^^
>arg : string
> : ^^^^^^
>length : number
> : ^^^^^^

});

Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,24 @@ const distantRes = distant({
},
consumer: (val) => {},
});

// https://github.com/microsoft/TypeScript/issues/62220

declare function testAsConst<T, D>(arg: {
input: D;
produce: (arg: D) => T;
consume: (arg: T) => number;
}): D;

const testAsConstResult1 = testAsConst({
input: 100,
produce: (arg) => arg.toString(),
consume: (arg) => arg.length,
} as const);

// included for comparison with the above
const testAsConstResult2 = testAsConst({
input: 100,
produce: (arg) => arg.toString(),
consume: (arg) => arg.length,
});
Loading