Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Negated types #29317

Closed
wants to merge 2 commits into from
Closed
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
326 changes: 276 additions & 50 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/compiler/factory.ts
Original file line number Diff line number Diff line change
@@ -876,8 +876,8 @@ namespace ts {
}

export function createTypeOperatorNode(type: TypeNode): TypeOperatorNode;
export function createTypeOperatorNode(operator: SyntaxKind.KeyOfKeyword | SyntaxKind.UniqueKeyword | SyntaxKind.ReadonlyKeyword, type: TypeNode): TypeOperatorNode;
export function createTypeOperatorNode(operatorOrType: SyntaxKind.KeyOfKeyword | SyntaxKind.UniqueKeyword | SyntaxKind.ReadonlyKeyword | TypeNode, type?: TypeNode) {
export function createTypeOperatorNode(operator: SyntaxKind.KeyOfKeyword | SyntaxKind.UniqueKeyword | SyntaxKind.ReadonlyKeyword | SyntaxKind.NotKeyword, type: TypeNode): TypeOperatorNode;
export function createTypeOperatorNode(operatorOrType: SyntaxKind.KeyOfKeyword | SyntaxKind.UniqueKeyword | SyntaxKind.ReadonlyKeyword | SyntaxKind.NotKeyword | TypeNode, type?: TypeNode) {
const node = createSynthesizedNode(SyntaxKind.TypeOperator) as TypeOperatorNode;
node.operator = typeof operatorOrType === "number" ? operatorOrType : SyntaxKind.KeyOfKeyword;
node.type = parenthesizeElementTypeMember(typeof operatorOrType === "number" ? type! : operatorOrType);
3 changes: 2 additions & 1 deletion src/compiler/parser.ts
Original file line number Diff line number Diff line change
@@ -3093,7 +3093,7 @@ namespace ts {
return finishNode(postfix);
}

function parseTypeOperator(operator: SyntaxKind.KeyOfKeyword | SyntaxKind.UniqueKeyword | SyntaxKind.ReadonlyKeyword) {
function parseTypeOperator(operator: SyntaxKind.KeyOfKeyword | SyntaxKind.UniqueKeyword | SyntaxKind.ReadonlyKeyword | SyntaxKind.NotKeyword) {
const node = <TypeOperatorNode>createNode(SyntaxKind.TypeOperator);
parseExpected(operator);
node.operator = operator;
@@ -3116,6 +3116,7 @@ namespace ts {
case SyntaxKind.KeyOfKeyword:
case SyntaxKind.UniqueKeyword:
case SyntaxKind.ReadonlyKeyword:
case SyntaxKind.NotKeyword:
return parseTypeOperator(operator);
case SyntaxKind.InferKeyword:
return parseInferType();
1 change: 1 addition & 0 deletions src/compiler/scanner.ts
Original file line number Diff line number Diff line change
@@ -98,6 +98,7 @@ namespace ts {
interface: SyntaxKind.InterfaceKeyword,
is: SyntaxKind.IsKeyword,
keyof: SyntaxKind.KeyOfKeyword,
not: SyntaxKind.NotKeyword,
let: SyntaxKind.LetKeyword,
module: SyntaxKind.ModuleKeyword,
namespace: SyntaxKind.NamespaceKeyword,
22 changes: 17 additions & 5 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
@@ -64,6 +64,7 @@ namespace ts {
| SyntaxKind.InterfaceKeyword
| SyntaxKind.IsKeyword
| SyntaxKind.KeyOfKeyword
| SyntaxKind.NotKeyword
| SyntaxKind.LetKeyword
| SyntaxKind.ModuleKeyword
| SyntaxKind.NamespaceKeyword
@@ -259,6 +260,7 @@ namespace ts {
InferKeyword,
IsKeyword,
KeyOfKeyword,
NotKeyword,
ModuleKeyword,
NamespaceKeyword,
NeverKeyword,
@@ -1242,7 +1244,7 @@ namespace ts {

export interface TypeOperatorNode extends TypeNode {
kind: SyntaxKind.TypeOperator;
operator: SyntaxKind.KeyOfKeyword | SyntaxKind.UniqueKeyword | SyntaxKind.ReadonlyKeyword;
operator: SyntaxKind.KeyOfKeyword | SyntaxKind.UniqueKeyword | SyntaxKind.ReadonlyKeyword | SyntaxKind.NotKeyword;
type: TypeNode;
}

@@ -3938,6 +3940,7 @@ namespace ts {
Conditional = 1 << 24, // T extends U ? X : Y
Substitution = 1 << 25, // Type parameter substitution
NonPrimitive = 1 << 26, // intrinsic object type
Negated = 1 << 27, // negated type

/* @internal */
AnyOrUnknown = Any | Unknown,
@@ -3967,7 +3970,7 @@ namespace ts {
UnionOrIntersection = Union | Intersection,
StructuredType = Object | Union | Intersection,
TypeVariable = TypeParameter | IndexedAccess,
InstantiableNonPrimitive = TypeVariable | Conditional | Substitution,
InstantiableNonPrimitive = TypeVariable | Conditional | Substitution | Negated,
InstantiablePrimitive = Index,
Instantiable = InstantiableNonPrimitive | InstantiablePrimitive,
StructuredOrInstantiable = StructuredType | Instantiable,
@@ -3983,16 +3986,16 @@ namespace ts {
NotPrimitiveUnion = Any | Unknown | Enum | Void | Never | StructuredOrInstantiable,
// The following flags are aggregated during union and intersection type construction
/* @internal */
IncludesMask = Any | Unknown | Primitive | Never | Object | Union | NonPrimitive,
IncludesMask = Any | Unknown | Primitive | Never | Object | Union | NonPrimitive | Negated | TypeVariable,
// The following flags are used for different purposes during union and intersection type construction
/* @internal */
IncludesStructuredOrInstantiable = TypeParameter,
IncludesStructuredOrInstantiable = Conditional,
/* @internal */
IncludesNonWideningType = Intersection,
/* @internal */
IncludesWildcard = Index,
/* @internal */
IncludesEmptyObject = IndexedAccess,
IncludesEmptyObject = Substitution,
// The following flag is used for different purposes by maybeTypeOfKind
/* @internal */
GenericMappedType = Never,
@@ -4072,6 +4075,11 @@ namespace ts {
export interface EnumType extends Type {
}

// Negated types (TypeFlags.Negated)
export interface NegatedType extends Type {
type: Type;
}

export const enum ObjectFlags {
Class = 1 << 0, // Class
Interface = 1 << 1, // Interface
@@ -4357,11 +4365,15 @@ namespace ts {
/* @internal */
resolvedInferredTrueType?: Type; // The `trueType` instantiated with the `combinedMapper`, if present
/* @internal */
resolvedInferredFalseType?: Type; // Likewise, the `falseType` with the `falseCombinedMapper`
/* @internal */
resolvedDefaultConstraint?: Type;
/* @internal */
mapper?: TypeMapper;
/* @internal */
combinedMapper?: TypeMapper;
/* @internal */
falseCombinedMapper?: TypeMapper;
}

// Type parameter substitution (TypeFlags.Substitution)
4 changes: 3 additions & 1 deletion src/harness/fourslash.ts
Original file line number Diff line number Diff line change
@@ -4560,7 +4560,7 @@ namespace FourSlashInterface {
export const keywords: ReadonlyArray<ExpectedCompletionEntryObject> = keywordsWithUndefined.filter(k => k.name !== "undefined");

export const typeKeywords: ReadonlyArray<ExpectedCompletionEntryObject> =
["false", "null", "true", "void", "any", "boolean", "keyof", "never", "number", "object", "string", "symbol", "undefined", "unique", "unknown", "bigint"].map(keywordEntry);
["false", "null", "true", "void", "any", "boolean", "keyof", "not", "never", "number", "object", "string", "symbol", "undefined", "unique", "unknown", "bigint"].map(keywordEntry);

const globalTypeDecls: ReadonlyArray<ExpectedCompletionEntryObject> = [
interfaceEntry("Symbol"),
@@ -4823,6 +4823,7 @@ namespace FourSlashInterface {
"infer",
"is",
"keyof",
"not",
"module",
"namespace",
"never",
@@ -5037,6 +5038,7 @@ namespace FourSlashInterface {
"infer",
"is",
"keyof",
"not",
"module",
"namespace",
"never",
1 change: 1 addition & 0 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
@@ -1220,6 +1220,7 @@ namespace ts {
SyntaxKind.BooleanKeyword,
SyntaxKind.FalseKeyword,
SyntaxKind.KeyOfKeyword,
SyntaxKind.NotKeyword,
SyntaxKind.NeverKeyword,
SyntaxKind.NullKeyword,
SyntaxKind.NumberKeyword,
Loading